It's super simple to remove whitespace from a string. To remove just the leading whitespace, you can use
trimStart(). To remove trailing whitespace, use
trimEnd(). Or remove it all with
const string = " hi "; string.trimStart(); // "hi " string.trimEnd(); // " hi" string.trim(); // "hi"
# Trim Return Value
All trim methods, will return a new string. That means your original string will be left intact.
const string = " hi "; string.trimStart(); // "hi " string.trimEnd(); // " hi" string.trim(); // "hi" string; // " hi "
# What is Whitespace
trim removes whitespace. So that is the white space created by:
- no-break space
- line terminator characters
# Trimming Line Terminator Characters
You might be wondering what are line terminator characters. Well, let's take a look at some examples.
'hi \n'.trim(); // "hi" 'hi \t'.trim(); // "hi" 'hi \r'.trim(); // "hi"
# Multi-line String
trim works with that, you bet it does 👍
const multiLine = ` hi `; multiline.trim(); // "hi"
# Trimming Multiple Words
trim only works for whitespace at the start and end. So it doesn't trim the space in between words.
' hi there '.trim(); // "hi there"
# Multi-Line of Multiple Words
Same with multi-line, it only trims the beginning of the first word and the ending of the last word.
const multiLine = ` hi there `; // Returns "hi there"
# Trim Aliases
# trimStart vs trimLeft
trimStart removes the leading white space. So all the whitespace from the beginning of a string to the first non-whitespace character will be gone.
You might also see people using
trimLeft(). Well, that's because it's an alias. It does the same thing.
const string = " hi "; string.trimStart(); // "hi " string.trimLeft(); // "hi "
# trimEnd vs trimRight
trimEnd removes the trailing white space. So all the whitespace from the end of a string will be gone. The alias of this method is
trimRight(). Again, they do the same thing.
const string = " hi "; string.trimEnd(); // " hi" string.trimRight(); // " hi"
# Which one should I use?
So which one should you use? Well, let's see what the ECMAScript Specification says:
trimEndare preferred. The
trimRightproperty are provided principally for compatibility with old code. It is recommended that the trimStart property be used in new ECMAScript code.
trimEnd for the win 👑
# Why are there aliases?
trimRight were introduced first. However, the committee decided to propose a word change to
trimEnd instead. That's because it's more consistent to their other built-in methods,
padEnd. Which makes sense to me, I think consistency is key and using the same language helps lessen confusion.
But for web compatibility reasons, they're keeping the old terms (
trimRight) as aliases. So if your code is using the older methods, no problem, they will still work 👍 However if you have the capacity, I'd recommend you changing it to use the official ones instead of the alias so you don't have two different methods floating around in your codebase. Remember it's all about that consistency 😎
# Trim Methods Use Case
I mostly used
trim(). Very handy to remove whitespace for a form input 👍
<input type="text" id="search" />
const inputValue = document.getElementById('search').value.trim();
You can also use it to remove odd whitespaces in a sentence and format it properly. Essentially creating your very own sentence prettier 💅
const uglySentence = "One Two Three Four "; const prettySentence = uglySentence .split(' ') // ["One", "", "Two", "", "", "Three", "Four", "", "", ""] .filter(word => word) // ["One", "Two", "Three", "Four"] .join(' '); // "One Two Three Four" // ✅ Result console.log(prettySentence); // "One Two Three Four"
I haven't used this before. But I can see where this can be handy. Take a Markdown file. You would want to preserve the leading whitespaces. But with the trailing whitespaces, you might want to automatically get rid of it so it doesn't render out a confusing or unexpected result for your user.
- List Item - Sub List Item - Sub List Item
I don't have a great example for this one. But if I stayed with the Markdown file example. We might want to prohibit nested listed items. However, we still want to preserve trailing whitespace. In Markdown, if you use insert two whitespaces, it will create a line break. I'm going to denote whitespaces with a dot
·, so you can see what's going on.
Markdown won't create a line break here
hi there // Result hi there
To force a line break, you can add 2 spaces.
hi·· there // Result hi there
So knowing this, you might not want to remove the trailing space. However, you still want to get rid of a nested list. In that case, then
trimStart might be the one for you.
# Browser Support
trim() is pretty awesome! However, it's a bit limited for
trimEnd, that's because they were introduced later on.
- trim: MDN Browser Compatibility Chart
- trimStart: MDN Browser Compatibility Chart
- trimEnd: MDN Browser Compatibility Chart
# Community Input
const string = ' hi '; string.replace(/ /g, ''); // "hi"
👆 Note: this solution will remove ALL whitespace from the string. To trim would be this:
let str = ' Samantha Ming '; let trimmedStr = str.replace(/^\s+ | \s+$/g, ''); console.log(trimmedStr); // "Samantha Ming"