JavaScript String trimEnd() Method


JavaScript

Introduction to trimEnd() in JavScript

Strings are one of the different datatypes within JavaScript that allows us to use and manage data within the language.

However, strings are often not stored the way we want them. Oftentimes, there are unnecessary characters that are present with the strings. One such unnecessary character is whitespace. Whitespaces are important within a string, but some whitespaces might be before or after a string that is unnecessary that’s where methods like trimEnd in JavaScript come in.

In this article, we will discuss how to remove whitespace using the trimEnd method and the alternatives for other whitespace situations.

 

Use trimEnd to remove whitespace

For different reasons, within your application, you might face strings like this " fly better with emirates " and will need to reduce it to the important characters, "fly better with emirates". That’s where methods like trimEnd might come in.

The trimEnd method helps us remove whitespace from the end of the string, and will return a new string without whitespace at the string end. So, the string worked upon by the method will remain as it is.

Let’s illustrate how the trimEnd method works by removing the whitespace at the end of the string with the examples given.

const str = "          fly better with emirates     ";

const newStr = str.trimEnd();

console.log(newStr);

Output

          fly better with emirates

Now, there is no whitespace at the end of the string, however, there is still whitespace at the start of the string.

 

Use trimStart to remove whitespace

To remove the whitespace from the start of the string, we need another method called trimStart. With it, we can remove the whitespace at the start and even chain the returned string from the trimEnd method to the trimStart method.

const str = "          fly better with emirates     ";

const newStr = str.trimEnd().trimStart();

console.log(newStr);

Output

fly better with emirates

Now, we don’t have any whitespace at the start or end of the string. However, there is a better way and that’s the trim method.

 

Use trim to remove whitespace

With the trim method, we can remove the whitespace from the start and the end of the string and return a new string with no ended whitespaces.

const str = "          fly better with emirates     ";

const newStr = str.trim();

console.log(newStr);

Output

fly better with emirates

 

Summary

Whitespaces characters are annoying and we need ways to remove them. Depending on where the whitespaces are, we can make use of the three methods to remove them. The trimEnd removes the whitespace from the string end and the trimStart removes the whitespace from the start of the string. However, if we need to remove the whitespaces from the start or the end of the string, we can use the trim method.

 

References

String.prototype.trimEnd() - JavaScript | MDN (mozilla.org)
String.prototype.trimStart() - JavaScript | MDN (mozilla.org)
String.prototype.trim() - JavaScript | MDN (mozilla.org)

 

Olorunfemi Akinlua

Olorunfemi Akinlua

He is boasting over five years of experience in JavaScript, specializing in technical content writing and UX design. With a keen focus on programming languages, he crafts compelling content and designs user-friendly interfaces to enhance digital experiences across various domains. You can connect with him on his LinkedIn profile.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment