JavaScript Reference ยท String
String.prototype.trimStart()
Reference for JavaScript String.prototype.trimStart(): remove whitespace from the beginning of a string, including the trimLeft() alias, with examples.
The trimStart() method returns a copy of a string with whitespace removed from the beginning only. Trailing whitespace stays. It uses the same definition of whitespace as trim(): spaces, tabs, line terminators, the non-breaking space and other Unicode space characters.
This is useful when leading indentation is noise but the end of the line matters, for example when processing indented text or removing the spaces before a value without touching trailing content.
" hello ".trimStart(); // "hello "
Try it
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Syntax
string.trimStart()
This method takes no parameters.
Return value: a new string with leading whitespace and line terminators removed.
Exceptions: none for strings.
Usage notes
- Alias:
trimLeft()is the older name for the same function. PrefertrimStart(), because "left" is wrong for right-to-left text, where the start of a line is on the right. - Same whitespace set as
trim(). The zero-width spaceU+200Bis not removed. - Inner whitespace and trailing whitespace are untouched.
- Immutable: it returns a new string, and the original is unchanged.
- Dedenting: to remove a common indentation from every line you need more than
trimStart(); compute the smallest indent and slice it off each line. - Numbers and leading zeros: it only removes whitespace, not zeros or other characters. For padding in the other direction see
padStart(). - Empty and blank strings give
"". - Non-strings do not have this method; convert with
String(value)first.
Browser support
String.prototype.trimStart() is available in all current browsers. It was added in ES2019, so check a compatibility table for very old browsers.