Lodash _.trimStart() Method

Beginner
⏱️ 7 min read
📚 Updated: Jul 2026
🎯 6 Examples + 3 Try It
String utilities

What You’ll Learn

By the end of this tutorial, you’ll use Lodash’s _.trimStart() confidently in real string workflows.

01

Core Syntax

Call _.trimStart(string, [chars]).

02

Leading Only

Removes characters from the start, not the end.

03

Custom Chars

Optional charset instead of whitespace.

04

Form Input

Strip accidental leading spaces from user text.

05

Alias

_.trimLeft is identical.

06

Production Tips

Normalize file contents and imported rows.

What Is _.trimStart()?

_.trimStart() removes characters from the beginning of a string while leaving the end untouched. Pair it with _.trimEnd() for one-sided cleanup, or use _.trim() when both sides need attention.

💡
Beginner tip

Think of _.trimStart(input) as “remove spaces the user accidentally typed before their answer, but keep trailing content as-is.”

📝 Syntax

javascript
_.trimStart(string, [chars])

Syntax Rules

  • string — The input string.
  • chars — Optional characters to remove from the start (default: whitespace).
  • Return value — New string without leading matched characters.
  • Alias_.trimLeft is the same function.
javascript
import trimStart from "lodash/trimStart";

const raw = "   Hello, World!";
const result = trimStart(raw);
// -> "Hello, World!"

⚡ Quick Reference

TaskCode patternResult
Whitespace_.trimStart(str)Leading spaces gone
Custom_.trimStart(str, "-")Strip leading hyphens
Alias_.trimLeft(str)Same function
File read_.trimStart(contents)BOM / indent cleanup
Both ends_.trim(str)Full edge trim
Nativestr.trimStart()Built-in equivalent
Mutates?
No

Returns new string

Side
Start only

Leading chars

Alias
trimLeft

Same API

Native
trimStart()

ES2019+

🧰 Parameters

string Required

String to process.

_.trimStart(input)
chars Optional

Character set for leading removal. Default is whitespace.

_.trimStart(code, "#")
return value New string

Copy without leading matched characters.

const clean = _.trimStart(raw)
trailing kept Preserved

End of string is never modified by trimStart.

// trailing spaces stay

Examples Gallery

Practical _.trimStart() patterns with copy-ready code and interactive Try It Yourself labs.

📚 Getting Started

Remove leading whitespace from a padded string.

Example 1 — Basic leading whitespace trim

Clean the start of an indented line.

javascript
const line = "   Hello, World!";
const trimmed = _.trimStart(line);

console.log(trimmed);
// -> "Hello, World!"
Try It Yourself

How It Works

Leading spaces are removed; any trailing content on the line stays.

📈 Practical Patterns

Form handling, data normalization, and file preprocessing.

Example 2 — Sanitize form input

Remove leading spaces users paste into a text field.

javascript
const input = "   draft title";
const safe = _.trimStart(input);

console.log(safe);
// -> "draft title"
Try It Yourself

How It Works

trimStart is ideal when only the beginning has accidental whitespace.

Example 3 — Strip leading hash marks

Remove comment prefixes from configuration lines.

javascript
const row = "---config";
const clean = _.trimStart(row, "-");
// -> "config"
Try It Yourself

How It Works

The chars argument lists which leading characters to strip.

Example 4 — Normalize imported data

Map trimStart over rows with inconsistent leading spaces.

javascript
const rows = ["  alpha", "\tbeta", "gamma"];
const clean = rows.map(_.trimStart);
// -> ["alpha", "beta", "gamma"]

How It Works

Batch normalize when ETL data has leading whitespace artifacts.

Example 5 — Validate then trim

Guard type before trimming user-provided strings.

javascript
function cleanInput(value) {
  if (typeof value !== "string") return "";
  return _.trimStart(value);
}

console.log(cleanInput("   ok"));
// -> "ok"

How It Works

Combine typeof checks with trimStart for robust form handlers.

🚀 Beyond the Basics

Native alternatives and related helpers.

Example 6 — Native trimStart() alternative

ES2019 String.prototype.trimStart() for whitespace.

javascript
const raw = "  hi  ";
const native = raw.trimStart();
const lodash = _.trimStart(raw);
// both -> "hi  "

How It Works

Native trimStart() handles default whitespace. Lodash supports custom leading character sets.

📋 Related string operations

Topic_.trimStart_.trimEnd_.trimtrimStart()
SideStart onlyEnd onlyBoth endsStart only
AliastrimLefttrimRighttrimLeft (legacy)
Custom charsYesYesYesWhitespace only
Best forLeading cleanupTrailing cleanupFull edge cleanupNative projects

🧠 How _.trimStart() Works

1

Receive string

Input is coerced to string when needed.

Input
2

Scan from start

Matching leading characters are skipped.

Scan
3

Slice remainder

Substring from first kept character to end.

Extract
=

Return copy

New string; trailing content unchanged.

Done

📝 Notes

  • _.trimStart() affects only the start of the string.
  • _.trimLeft is an alias.
  • Pass chars for custom leading character removal.
  • Use _.trimEnd() for the opposite side.
  • Use _.trim() when both ends need cleaning.
  • Native trimStart() is available in ES2019+.

Conclusion

_.trimStart() cleans leading whitespace and custom characters without touching the end of your string. Use it for form input, imported rows, and file preprocessing when only the beginning needs normalization.

💡 Best Practices

✅ Do

  • Assign the return value—strings are immutable
  • Combine with related helpers when building normalization pipelines
  • Validate user input types before transforming
  • Test Unicode and locale-sensitive strings when relevant
  • Use native string.trimStart() when Lodash is not already imported

❌ Don’t

  • Expect the original string variable to change in place
  • Assume behavior matches locale-aware toLocaleLowerCase without testing
  • Trim or change casing before checking for empty input when order matters
  • Import all of Lodash for a single call if tree-shaking a tiny bundle
  • Forget to handle null/undefined coercion edge cases

Key Takeaways

01

Start only

Leading chars removed.

Basics
02

Trailing kept

End preserved.

Behavior
03

trimLeft

Alias name.

API
04

Forms

Sanitize user input.

Pattern
05

truncate

Next string helper.

Next step

❓ Frequently Asked Questions

_.trimStart() removes leading characters (whitespace by default) from the start of a string and returns the result.
No. Trailing characters—including trailing whitespace—are preserved.
Yes. _.trimStart(str, '-') removes leading hyphens.
Lodash exports _.trimLeft as an alias for the same function.
Use _.trim() when both leading and trailing characters need removal.
Yes. String.prototype.trimStart() removes leading whitespace in modern JavaScript.
Did you know?

Lodash exports _.trimLeft as an alias for _.trimStart. Next in this track: _.truncate() for shortening long strings with an omission marker.

Practice _.trimStart() in the Live Editor

Open Try It, run the examples, and experiment with your own strings.

Open Try It editor →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful