If you want to remove the whitespaces from the beginning of a string, then use the trimStart()
method. The trimStart() method does not modify the original string and returns a new string that is left-trimmed.
The syntax for the trimStart() method is:
let leftTrimmedString = originalString.trimStart();
In JavaScript, the following characters are considered whitespaces:
The following example explains how to use trimStart()
method.
let str = " JavaScript String "; let leftTrimmedString = str.trimStart(); console.log('Left-trimmed String'); console.log(leftTrimmedString); console.log('Original String'); console.log(str);
Output
Left-trimmed String "JavaScript String " Original String " JavaScript String "