Nerd For Tech
Published in

Nerd For Tech

Basics of Javascript · String · trim() (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”.

Hi there fellow developer newbs. We are slowly getting towards the end of this series. Only 4 more methods to cover. One of them is trim() which lets us take care of unnecessary empty spaces before and after our desired string content. Let’s see how it works.

trim() method removes whitespace from both sides 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 does not change the original string and also does not have any parameters.

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

Let’s see the method in action 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.trim() + "'"
// OUTPUT:
// TRIMMED CONTENT: 'Dev Newbs!'
// empty string
" EMPTY STRING: '" + "".trim() + "'"
// OUTPUT:
// EMPTY STRING: ''
// string with only whitespace
"ONLY WHITESPACES: '" + "\t\n \r".trim() + "'"
// 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 not at beginning or end are kept
" TRIMMED CONTENT: \n'" + alphabet.trim() + "'"
// OUTPUT:
// TRIMMED CONTENT:
// 'A B
// C D ...'
try {
console.log(" NULL: '" + null.trim() + "'");
}
catch(err) {
console.log(err);
}
// OUTPUT:
// TypeError: Cannot read property 'trim' of null
try {
console.log(" UNDEFINED: '" + undefined.trim() + "'");
}
catch(err) {
console.log(err);
}
// OUTPUT:
// TypeError: Cannot read property 'trim' of undefined

We can see in the first case that all the whitespaces from the beginning and the end are gone after trim is done. But we can get rid of more than just spaces with trim(). Second case shows us that we actually remove all the whitespaces including tabs and newlines. But only from the beginning and end. If they are within a string body that actually has content, it is kept.

On the other hand, if we have an empty string or a string that only contains whitespace, what we get is an empty string as result.

The last case to cover is using trim on “null”, “undefined” or other special objects. If you try to do so, you will get a TypeError. Which makes sense, since there is nothing to trim there.

OK, that was the trim() method. One of the last methods of the String built-in object. I salute you for your persistence and devotion. See you with a few remaining methods 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
Jakub Korch

Jakub Korch

Web enthusiast, programmer, husband and a father. Wannabe entrepreneur. You name it.