FrontendDeveloper.in

ECMAScript question detail

String trimStart and trimEnd

In order to make consistency with padStart/padEnd, ES2019 provided the standard functions named as trimStart and trimEnd to trim white spaces on the beginning and ending of a string. However for web compatibility(avoid any breakage) trimLeft and trimRight will be an alias for trimStart and trimEnd respectively.

Let's see the usage with an example,

//Prior ES2019
let messageOne = "   Hello World!!    ";
console.log(messageOne.trimLeft()); //Hello World!!
console.log(messageOne.trimRight()); //   Hello World!!

//With ES2019
let messageTwo = "   Hello World!!    ";
console.log(messageTwo.trimStart()); //Hello World!!
console.log(messageTwo.trimEnd()); //   Hello World!!
Back to all ECMAScript questions
Get LinkedIn Premium at Rs 399