ECMAScript10 (ES2019) added trimStart() / trimLeft() and trimEnd() / trimRight() functions to the string prototype.
Why trim strings?
String prototype functions to trim whitespace
New features added in ECMAScript 10 (ES2019)
The functions trimStart() and trimEnd() are only available in ES2019, meaning that they work with modern browsers but not older versions.
The simplified version of ECMAScript 10 browser compatibility is that you can use these functions in any browser except Internet Explorer.
How to use trim(), trimStart(), and trimEnd()
Here is a code example using String.prototype.trim(), String.prototype.trimStart(), and String.prototype.trimEnd():
Three things to know about trimming strings
- The trimStart() / trimLeft() methods return the string stripped of whitespace from its left end.
- The trimEnd() / trimRight() methods return the string stripped of whitespace from its right end.
- Neither trimStart() / trimLeft() nor trimEnd() / trimRight() affect the value of the string itself.