The trimStart() method of String values removes whitespace from the beginning of this string and returns a new string, without modifying the original string. trimLeft() is an alias of this method.
The trimStart() method of String values removes whitespace from the beginning of this string and returns a new string, without modifying the original string. trimLeft() is an alias of this method.
trimStart() trimLeft()
None.
A new string representing str stripped of whitespace from its beginning (left side). Whitespace is defined as white space characters plus line terminators.
If the beginning of str has no whitespace, a new string is still returned (essentially a copy of str).
After trim() was standardized, engines also implemented the non-standard method trimLeft. However, for consistency with padStart(), when the method got standardized, its name was chosen as trimStart. For web compatibility reasons, trimLeft remains as an alias to trimStart, and they refer to the exact same function object. In some engines this means:
String.prototype.trimLeft.name === "trimStart";
The following example trims whitespace from the start of str, but not from its end.
let str = " foo "; console.log(str.length); // 8 str = str.trimStart(); console.log(str.length); // 5 console.log(str); // 'foo '
| Desktop | Mobile | Server | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Opera | Safari | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | WebView Android | Deno | Node.js | ||
trimStart |
664 | 7912 | 613.5 | 5315 | 12 | 6618 | 614 | 4714 | 12 | 9.01.0 | 66≤37 | 1.01.0 | 10.0.00.12.0 | |
© 2005–2023 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/trimStart