The trimStart() method of the String class in JavaScript removes whitespace from the beginning of a string. It is an alias for trimLeft().
There are 2 variations for the syntax of String.trimStart() method. They are:
trimStart()
This method removes whitespace from the beginning of the string.
Returns value of type String
.
trimLeft()
This method alias for trimStart(). Removes whitespace from the beginning of the string.
Returns value of type String
.
In JavaScript, the trimStart()
method removes whitespace from the beginning of a string.
For example,
str
with the value ' Hello World'
which has leading whitespace.trimStart()
method to remove the leading whitespace.newStr
.newStr
to the console using the console.log()
method.const str = ' Hello World';
const newStr = str.trimStart();
console.log(newStr);
Hello World
In JavaScript, the trimLeft()
method, an alias for trimStart()
, removes whitespace from the beginning of a string.
For example,
str
with the value ' Hello World'
which has leading whitespace.trimLeft()
method to remove the leading whitespace.newStr
.newStr
to the console using the console.log()
method.const str = ' Hello World';
const newStr = str.trimLeft();
console.log(newStr);
Hello World
In JavaScript, both trimStart()
and trimLeft()
methods perform the same function of removing whitespace from the beginning of a string.
For example,
str
with the value ' Hello World'
which has leading whitespace.trimStart()
method to remove the leading whitespace and store the result in newStr1
.trimLeft()
method to remove the leading whitespace and store the result in newStr2
.newStr1
and newStr2
to the console using the console.log()
method.const str = ' Hello World';
const newStr1 = str.trimStart();
const newStr2 = str.trimLeft();
console.log(newStr1); // Hello World
console.log(newStr2); // Hello World
Hello World Hello World
In this JavaScript tutorial, we learned about trimStart() method of String: the syntax and few working examples with output and detailed explanation for each example.