JavaScript Basics

JavaScript Advanced

JavaScript Arrays

JavaScript Functions

JavaScript Objects

JavaScript DOM

JavaScript String

How to use the String trimStart() method in JavaScript?

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:

  • Spaces
  • Tabs
  • Newline
  • Carriage return
  • Form feed

The following example explains how to use trimStart() method.

let str = "   JavaScript String     ";
let leftTrimmedString = str.trimStart();

console.log('Left-trimmed String');
console.log('Original String');


Left-trimmed String
"JavaScript String     "
Original String
"   JavaScript String     "

Recommended Posts