transformation
String.prototype.trimStart
Returns a new string with leading whitespace removed. Added in ES2019 (aliased as trimLeft for legacy code).
str.trimStart() Parameters
| Parameter | Purpose |
|---|---|
| (no arguments) | Method takes no parameters |
| returns | New string with leading whitespace stripped |
Examples
console.log(' hello '.trimStart()); Prints 'hello '
console.log('\t\n abc'.trimStart()); Prints 'abc'
console.log('abc '.trimStart()); Prints 'abc '
console.log(' '.trimStart()); Prints '' (all whitespace removed)
Gotcha
trimLeft is a legacy alias — prefer trimStart. Only affects the beginning; trailing whitespace is preserved.
Related methods
String.prototype.trim
Returns a new string with whitespace removed from both ends. Whitespace includes spaces, tabs, newlines, and Unicode whitespace like non-breaking space.
String.prototype.trimEnd
Returns a new string with trailing whitespace removed. Added in ES2019 (aliased as trimRight for legacy code).
String.prototype.padStart
Returns a new string padded on the left with padString (default ' ') until it reaches targetLength. Added in ES2017; the original is returned unchanged if it is already long enough.
String.prototype.replace
Returns a new string with the first match of pattern replaced. Pattern can be a string (only the first occurrence) or a RegExp (all occurrences when the g flag is set).
String.prototype.replaceAll
Returns a new string with every occurrence of pattern replaced. Added in ES2021; if pattern is a RegExp it must have the g flag or replaceAll throws TypeError.