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'
String.prototype.trim{Start,End}
support #
- Chrome: supported since version 66
- Firefox: supported since version 61
- Safari: supported since version 12
- Node.js: supported since version 8
- Babel: supported