Javascript String provides trim method to remove the trailing and leading method.

ES10 String - trimStart,trimEnd methods

ES10 introduced two methods to String class to the latest javascript. These two string methods remove space from a given string.

  • String.trimStart() - removes the whitespaces from starting position of a given string
  • String.trimEnd() - removes the whitespaces from end position of a input string

Here is an example.

let str1=" firstString";
let str2="secondString ";

str1.length // outputs 13
str1.trimStart(); // outputs firstString without space
str1.length // outputs 12

str2.length // outputs 13
str2.trimEnd(); // outputs secondString without space
str2.length // outputs 12