STRING TRIMMING METHODS
ECMAScript 2019 added two methods to the String
prototype, trimStart()
and trimEnd()
, which allow for targeted whitespace removal. These methods are intended to replace trimLeft()
and trimRight()
which have ambiguous meaning in the context of right-to-left languages such as Arabic and Hebrew.
These two methods are effectively the opposite of padStart()
and padEnd()
with a single space character. The following example adds whitespace to a string and then removes it on either side:
let s = ' foo ';
console.log(s.trimStart()); // "foo "
console.log(s.trimEnd()); // " foo"