JavaScript String trimStart

Summary: in this tutorial, you’ll learn how to use the JavaScript String trimStart() method to remove whitespace from the beginning of a string.

To remove the whitespace characters from the beginning of a string, you use the trimStart() method:

let newString = originalString.trimStart();Code language: JavaScript (javascript)

The trimStart() method returns a new string from the original string with the leading whitespace characters removed. The trimStart() method doesn’t change the original string.

The following characters are the whitespace characters in JavaScript:

  • A space character
  • A tab character
  • A carriage return character
  • A new line character
  • A vertical tab character
  • A form feed character

The following example shows how to use the trimStart() to remove whitespace characters from the beginning of a string:

const str = '   JavaScript   ';
const result = str.trimStart();

console.log({ str });
console.log({ result });
Code language: JavaScript (javascript)

Output:

{ str: '   JavaScript   ' }
{ result: 'JavaScript   ' }
Code language: JavaScript (javascript)

The trimLeft() method is an alias of the trimStart() method. Therefore, the trimLeft() has the same functionality as the trimStart() method. It’s recommended that you use the trimStart() method.

Summary

  • The trimStart() returns a new string from the original string with the leading whitespace characters removed.
  • The trimLeft() method is an alias of the trimStart() method.
Was this tutorial helpful ?