String.prototype.trimStart()
Removes whitespace and line terminators from the beginning of a string.
trimStart()
trimLeft()- Added in JavaScript ES2019
Return value
stringA new string representing str stripped of whitespace from its beginning (left side). If the beginning of str has no whitespace, a new string is still returned (essentially a copy of str).
Changelog
- PHP ES2019Standardised as trimStart, chosen for consistency with padStart(). The earlier non-standard trimLeft remains as an alias to the exact same function object, so String.prototype.trimLeft.name === "trimStart".
trimStart() removes whitespace from the front of a string only, leaving the end exactly as it was. Use it when trailing content is meaningful — dedenting a source line while keeping its trailing newline, or stripping an indent without disturbing alignment padding.
trimLeft is the same function
Engines shipped the non-standard trimLeft before the method was standardised. For web compatibility it survives as an alias pointing at the identical function object, which is why String.prototype.trimLeft === String.prototype.trimStart is true and trimLeft.name reports "trimStart". The standard name was chosen to pair with padStart(). Write trimStart in new code.
Start means index 0, not visual left
Despite the old name, this operates on the beginning of the string in memory order. For right-to-left scripts that is the visually rightmost end, so "left" was always a misleading label.
Whitespace definition
The same set trim() uses: all Unicode white space characters plus line terminators, including tabs, non-breaking space and U+FEFF. A zero-width space (U+200B) is not whitespace and will not be removed.
Example
When to use it
- Strip indentation from each line of a template literal
- Remove leading padding while keeping a trailing newline
- Normalise a pasted identifier without touching the end
- Left-align text read from a fixed-width column
Frequently asked questions
What is the difference between trimStart and trimLeft?
There is none — trimLeft is a web-compatibility alias pointing at the exact same function object as trimStart, so the two are strictly equal and trimLeft.name is "trimStart". Use trimStart, the standard name chosen to match padStart().
How do I remove only the leading spaces of a string?
Call str.trimStart(), which strips whitespace and line terminators from the beginning and leaves the end untouched. Assign the result, since strings are immutable and the original variable is unchanged.
Does trimStart remove a zero-width space?
No. U+200B ZERO WIDTH SPACE is not in the specification's whitespace set, so trimStart() leaves it in place even though it is invisible. Strip it explicitly with replace(/^[]+/, "") if your input carries one.
More examples
Dedenting a template literal
Every line loses its leading spaces, including the extra indent on "nested", so relative nesting is flattened. That is the tradeoff of a per-line trimStart: it is simple, but a real dedent has to measure the common indent and slice that many characters instead.
const block = `
line one
line two
nested
`;
const dedented = block
.split("\n")
.map((l) => l.trimStart())
.join("\n");
console.log(JSON.stringify(dedented));Keeping the trailing newline
Each trimStart result keeps its \n, so the lines can be rejoined without rebuilding the terminators. The final line shows trim() removing the newline too — fine for display, wrong when you are rewriting a file in place.
const lines = [" alpha\n", " beta\n", "\tgamma\n"];
for (const l of lines) {
console.log(JSON.stringify(l.trimStart()));
}
// trim() would take the newline as well
console.log(JSON.stringify(lines[0].trim()));Whitespace that is not whitespace
Non-breaking space and the byte-order mark are both in the whitespace set and disappear. The zero-width space U+200B is not, so it survives and the length stays 2 — the usual explanation for a string that looks trimmed but still fails an equality check.
console.log(JSON.stringify(" x".trimStart())); // NBSP removed
console.log(JSON.stringify("x".trimStart())); // BOM removed
console.log(JSON.stringify("x".trimStart())); // zero-width kept
console.log("x".trimStart().length);
Discussion