`String.prototype.trimStart` and `String.prototype.trimEnd`
· One min read
ES2019 introduces String.prototype.trimStart()
and String.prototype.trimEnd()
:
const string = ' hello world ';
string.trimStart();
// → 'hello world '
string.trimEnd();
// → ' hello world'
string.trim(); // ES5
// → 'hello world'
This functionality was previously available through the non-standard trimLeft()
and trimRight()
methods, which remain as aliases of the new methods for backward compatibility.
const string = ' hello world ';
string.trimStart();
// → 'hello world '
string.trimLeft();
// → 'hello world '
string.trimEnd();
// → ' hello world'
string.trimRight();
// → ' hello world'
string.trim(); // ES5
// → 'hello world'