How to Remove Spaces from String in JavaScript?

Do you ever get inputs like ' J a v aSc   r ip   t  '. Don’t worry, this article will show you 2 ways to remove spaces from a string in JavaScript.

Spoiler alert, the second method is better. Well, it is mostly subjective, but I prefer the second method.

Split and Join

This is a simple and pretty straightforward method.

function removeSpace(str) {
    return str.split(' ').join('');

let result = removeSpace(' J a v aSc   r ip   t   ');
console.log(result); // JavaScript

First, you split the string into an array with split() ignoring all the spaces along the way by passing ' ' as a parameter. The split() method should give you this result ['', 'J', 'a', 'v', 'aSc', '', '', 'r', 'ip', '', '', 't', '', '', ''].

You’ll notice that the spaces aren’t actually gone yet, but they’re turned into empty stings instead. Don’t worry, we’ll get rid of the with join().

join('') lets us combine the array elements into a string. It’s important that we pass '' as a parameter as that indicates we join the string with ''. If you don’t do it, your array will be joined with ',' instead and you result will look like this ,J,a,v,aSc,,,r,ip,,,t,,,.

Time Complexity : O(n)

Space Complexity : O(n)

Split and join each has to iterate through all the elements in the string or the array, hence the O(n) time. As for the space complexity, we also store the array elements temporarily to join it later on, the amount of the elements is equal to the string length, so it is O(n).


The second method is using regular expressions. As a short intro or a short reminder, regex or regular expressions are patterns that are used to match combinations of characters in a string.

function removeSpace(str) {
    return str.replace(/\s/g, '');

let result = removeSpace(' J a v aSc   r ip   t   ');
console.log(result); // JavaScript

This method is also simple, but you do need an understanding of regex first.

Let’s break the code down

replace() changes all your characters that match the pattern in the first parameter with the string in the second parameter.

The first parameter (/\s/g) is the pattern to search for white space in your string. the two / is the regex syntax. \s is for white space and g which stands for global means look for all characters that match the pattern. If you don’t use g, it’ll only look for the first character that matches the pattern.

The second parameter ('') is simply the string that you want to replace the pattern with.

Time Complexity : O(n)

Space Complexity : O(1)

Well, determining the complexity for regex operations is quite tricky. The complexity also depends on the underlying mechanism of the browser it runs on.

So, generally the space complexity would be O(n) since it has to traverse through the length of the string and the space complexity would be O(1) because it doesn’t store anything.


That’s how you can remove spaces from a string in JavaScript. There are other methods to to this, with trim() (but there are downsides to this), reduce(), loops and others. But, I prefer to show the two best ways according to me instead of giving you a lot of items to read through so you can quickly get what you came for.

Hope that helps!

If you want another explanation on string topic in JavaScript, check out How to Reverse a String in JavaScript and How to Check for a Palindrome in JavaScript?.

If you have a better solution be it code-wise or efficiency-wise, please do comment below!

Check out the rest of my blog for more helpful contents on Data Structures and Algorithms in JavaScript!

See you next post!

Leave a Comment