How to Trim Strings in JavaScript
ECMAScript10 (ES2019) added trimStart() / trimLeft() and trimEnd() / trimRight() functions to the string prototype.
Why trim strings?
A trim function is useful for removing extra spaces typed by users. Often times, users aren’t even aware that they typed extra spaces. This fact could also lead to login problems if, for example, a user registered himself with trailing whitespace. — “Trimming Strings in JavaScript” by @AurelioDeRosa
Trimming strings is a common type of string manipulation used by computer programmers in any language. Recent features added to the ECMAScript specification made trimming strings easier than ever in JavaScript.
String prototype functions to trim whitespace
For many years, JavaScript has had String.prototype.trim(), a built-in function to remove both leading & trailing whitespace.
Javascript’s trimming functions are used with the






