Nerd For Tech
Published in

Nerd For Tech

Basics of Javascript · String · trimStart() (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. Only 2 more methods to cover. Today, we are looking at another lazy sibling of the trim() method which only trims the whitespaces from the beginning of the string. Wow, this trim() has some lazy family members. But nevertheless — let’s cover it quickly.

trimStart() method removes whitespace from the beginning 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 trimLeft() that can be used interchangeably.

TrimStart() 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 beginning.

Let’s see it pulling its weight in example 1.

const greeting = "     Dev Newbs!     ";// untrimmed original
"UNTRIMMED CONTENT: '" + greeting + "'"
// OUTPUT:
// UNTRIMMED CONTENT: ' Dev Newbs! '
// whitespace trimmed from the beginning only
" TRIMMED CONTENT: '" + greeting.trimStart() + "'"
// OUTPUT:
// TRIMMED CONTENT: 'Dev Newbs! '
// alternative method name
" ALTERNATIVE NAME: '" + greeting.trimLeft() + "'"
// OUTPUT:
// ALTERNATIVE NAME: 'Dev Newbs! '
// empty string
" EMPTY STRING: '" + "".trimStart() + "'"
// OUTPUT:
// EMPTY STRING: ''
// string with only whitespace
"ONLY WHITESPACES: '" + "\t\n \r".trimStart() + "'"
// 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.trimStart() + "'"
// OUTPUT:
// TRIMMED CONTENT:
// 'A B
// C D ...
// '
try {
console.log(" NULL: '" + null.trimStart() + "'");
}
catch(err) {
console.log(err);
}
// OUTPUT:
// TypeError: Cannot read property 'trimStart' of null
try {
console.log(" UNDEFINED: '" + undefined.trimStart() + "'");
}
catch(err) {
console.log(err);
}
// OUTPUT:
// TypeError: Cannot read property 'trimStart' of undefined

We can see in the first case that all the whitespaces from the beginning of the string are gone. 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 trimStart(). Another case shows us that we actually remove all the whitespaces including tabs and newlines. But only from the beginning.

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

That wraps up trimStart(). As always, thank you so much for your attention. I will see you tomorrow with the last method of the String series.

--

--

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