# How to Trim String in JavaScript

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 trim() ๐Ÿ™Œ

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

So trim removes whitespace. So that is the white space created by:

  • space
  • tab
  • 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

In JavaScript, you can easily create multi-line strings using the Template Literals. So if you're wondering if trim works with that, you bet it does ๐Ÿ‘

const multiLine = `


multiline.trim(); // "hi"

# Trimming Multiple Words

Remember 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 = `



// Returns



# 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:

The property trimStart nad trimEnd are preferred. The trimLeft and trimRight property are provided principally for compatibility with old code. It is recommended that the trimStart property be used in new ECMAScript code.

trimStart and trimEnd for the win ๐Ÿ‘‘

# Why are there aliases?

So trimLeft and trimRight were introduced first. However, the committee decided to propose a word change to trimStart and trimEnd instead. That's because it's more consistent to their other built-in methods, padStart and 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 (trimLeft and 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


// Result
hi there

To force a line break, you can add 2 spaces.


// Result

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

Support for trim() is pretty awesome! However, it's a bit limited for trimStart and trimEnd, that's because they were introduced later on.

Browser trim trimStart trimEnd
Chrome โœ… โœ… โœ…
Firefox โœ… โœ… โœ…
Safari โœ… โœ… โœ…
Edge โœ… โœ… โœ…
Internet Explorer โœ… โŒ โŒ

# 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"

Thanks @Jalaj

# Resources

Related Tidbits