Nerd For Tech
Published in

Nerd For Tech

Basics of Javascript · String · trimEnd() (method)

This article is a transcript of my free youtube series about basics of web development. If you prefer watching over reading, feel free to visit my channel “Dev Newbs”.

Hello dev newbs. 3 more methods to cover. Today, we are looking at a lazy sibling of the trim() method which only trims the whitespace from the end of the string. Let’s get into it.

trimEnd() method removes whitespace from the end of a string. Whitespace in our context is all the whitespace characters (space, tab, no-break space, etc.) and all the line terminator characters (line feed, carriage return, etc.).

The method also has an alias name of trimRight() that can be used interchangeably.

TrimEnd() does not change the original string and does not have any parameters.

The return value is a string, representing the string with removed whitespace from the end.

Let’s see how it works in example 1.

const greeting = "     Dev Newbs!     ";// untrimmed original
"UNTRIMMED CONTENT: '" + greeting + "'"
// OUTPUT:
// UNTRIMMED CONTENT: ' Dev Newbs! '
// whitespace trimmed from beginning and end
" TRIMMED CONTENT: '" + greeting.trimEnd() + "'"
// OUTPUT:
// TRIMMED CONTENT: ' Dev Newbs!'
// alternative method name
" ALTERNATIVE NAME: '" + greeting.trimRight() + "'"
// OUTPUT:
// ALTERNATIVE NAME: ' Dev Newbs!'
// empty string
" EMPTY STRING: '" + "".trimEnd() + "'"
// OUTPUT:
// EMPTY STRING: ''
// string with only whitespace
"ONLY WHITESPACES: '" + "\t\n \r".trimEnd() + "'"
// OUTPUT:
// ONLY WHITESPACES: ''
const alphabet = "\t\n A B \n C D ... \n";// untrimmed original
"UNTRIMMED CONTENT: \n'" + alphabet + "'"
// OUTPUT:
// UNTRIMMED CONTENT:
// '
// A B
// C D ...
// '
// whitespaces in "the middle" are kept
" TRIMMED CONTENT: \n'" + alphabet.trimEnd() + "'"
// OUTPUT:
// TRIMMED CONTENT:
// '
// A B
// C D ...'
try {
console.log(" NULL: '" + null.trimEnd() + "'");
}
catch(err) {
console.log(err);
}
// OUTPUT:
// TypeError: Cannot read property 'trimEnd' of null
try {
console.log(" UNDEFINED: '" + undefined.trimEnd() + "'");
}
catch(err) {
console.log(err);
}
// OUTPUT:
// TypeError: Cannot read property 'trimEnd' of undefined

We can see in the first case that all the whitespaces from the end are gone after trimEnd is done. Also the alternative name of the method gets us identical results.

If we have an empty string or a string that only contains whitespaces, what we get is an empty string as result.

We can of course get rid of more than just spaces with trimEnd(). Another case shows us that we actually remove all the whitespaces including tabs and newlines. But only from the end.

The last case to cover is using trimEnd() on “null”, “undefined” or other special objects. If you try to do so, you will get a TypeError.

TrimEnd() is done. Only a handful is left. As usual, thanks for your attention and I will see you with the next method soon.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store