avatarDr. Derek Austin 🥳

Summary

This context provides information on how to trim strings in JavaScript using various built-in functions such as trim(), trimStart(), and trimEnd().

Abstract

The context discusses the importance of trimming strings in JavaScript, particularly to remove extra spaces typed by users. It highlights the built-in functions available for string trimming, including trim(), which has been available for many years, and trimStart() and trimEnd(), which were added in ECMAScript 10 (ES2019). The context also provides code examples and browser compatibility information for these functions.

Bullet points

  • Trimming strings is useful for removing extra spaces typed by users, often unknowingly.
  • JavaScript has a built-in function String.prototype.trim() to remove both leading and trailing whitespace.
  • ECMAScript 10 (ES2019) added String.prototype.trimStart() and String.prototype.trimEnd() to remove only leading or trailing whitespace, respectively.
  • These new functions are only available in modern browsers and not in older versions, such as Internet Explorer.
  • 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.

How to Trim Strings in JavaScript

ECMAScript10 (ES2019) added trimStart() / trimLeft() and trimEnd() / trimRight() functions to the string prototype.

(Photo by Aditya Wardhana on Unsplash)

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 e>.dot-reference operator because they are part of the built-in JavaScript string prototype.

New features added in ECMAScript 10 (ES2019)

ES10 added String.prototype.trimStart() and String.prototype.trimEnd() to remove only leading or trailing whitespace, respectively.

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.
(Photo by Fleur on Unsplash)

Additional resources:

  • The w3schools page for String.prototype.trim() compares using the trim function to using a regular expression in JavaScript:
  • The MDN web docs are a great place to learn about the String prototype that is built-in to JavaScript:

Dr. Derek Austin is the author of Career Programming: How You Can Become a Successful 6-Figure Programmer in 6 Months, now available on Amazon.

Programming
JavaScript
Coding
Software Development
Web Development
Recommended from ReadMedium