String in JavaScript

Published on 14 Feb, 2021

String Instance Methods

trimStart() ES10

trimStart() method removes whitespace from the beginning of a string and returns the trimmed string.

const str = "     Backbencher     ";
const trimmed = str.trimStart();console.log(trimmed); // "Backbencher     "

Technically trimStart() and trimLeft() are same. trimLeft() is an alias to trimStart().

console.log(String.prototype.trimLeft.name); // "trimStart"

trimEnd() ES10

trimEnd() method removes whitespace from the end of a string and returns the trimmed string.

const str = "     Backbencher     ";
const trimmed = str.trimEnd();console.log(trimmed); // "     Backbencher"

Technically trimEnd() and trimRight() are same. trimRight() is an alias to trimEnd().

console.log(String.prototype.trimRight.name); // "trimEnd"