AppDividend
Latest Code Tutorials

Javascript String trimStart() trimEnd() Tutorial With Example

0

Javascript String trimStart() trimEnd() Tutorial With Example is today’s topic. The trimStart() method removes whitespace from the beginning of the string. The trimLeft() is an alias of this method. The trimEnd() method removes whitespace from the end of a string. The trimRight() is an alias of this method.

Javascript String trimStart() trimEnd() Tutorial

Javascript String trimStart() or trimLeft() do not affect the value of the string itself.

First, let’s see the example of Javascript String trimStart() method.

// app.js

let app = '   Human Nature!   ';
console.log(app);
console.log(app.trimStart());

The output of the above code is following.

 

Javascript String trimStart() Tutorial

It returns a new string with removed white space from the start of the original string.

Javascript String trimEnd() or trimRight() do not affect the value of the string itself.

Okay, now let’s see the example of a Javascript String trimEnd() method.

// app.js

let app = '   Human Nature!   ';
console.log(app);
console.log(app.trimEnd());

The output is following.

 

Javascript String trimStart() trimEnd() Tutorial

It returns a new string representing the calling string stripped of whitespace from its (right) end.

We can count the length of the strings to see if the whitespaces are trimmed or not.

// app.js

let app = '   Human Nature!   ';
console.log('Before trimmed', app.length);
console.log('After trimmed',app.trimEnd().length);

The output is following.

 

Javascript String trimStart() trimEnd() Tutorial With Example

Finally, Javascript String trimStart() trimEnd() Tutorial With Example is over.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.