AppDividend
Latest Code Tutorials

Javascript String trimStart() trimEnd() Tutorial With Example

Javascript String trimStart() is an inbuilt function that removes whitespace from the beginning of the string. Javascript string trimLeft() is an alias of this method. Javascript string trimEnd() is an inbuilt function that method removes whitespace from the end of a string. The trimRight() is an alias of this method.

Javascript string trimStart() trimEnd()

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

First, let’s see the example of the 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() does 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());

See the output

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);

See the output.

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.