How To Remove All Spaces From A String In JavaScript

Remove all spaces from a string in javascript

One of the most common problems that you will have to face when working with strings is to remove all spaces from a string in JavaScript. If you still don’t know how to do it then please don’t panic because in this tutorial, we will show you some of the most basic and common ways to get it done. 

Remove all spaces from a string in JavaScript

Method 1: Using a for loop

We will start with the most basic and straightforward way, which is using a for loop. Our idea is: We will scan through every element of the string. If the element is not a space, we will push it into our new string. Now, let’s demonstrate our idea in JavaScript:  

var myString = 'Today is a bad day in Miami';
var result = '';

for (let i = 0; i < myString.length; i++) {
    if (myString[i] !== ' ') {
        result += myString[i];
    } else continue;
};

console.log(result);

Output:

TodayisabaddayinMiami

Method 2: Using the string.replace() method

The string.replace() method will search the string for a value and return a new string with that value replaced.

Syntax

string.replace(searchValue, newValue)

Parameters:

  • searchValue: the value to be searched for 
  • newValue: the value to be replaced with

Return value: the string in which the new value has been replaced. 

Example 1: 

var oldString = 'Hello everyone, I am Robbert!';
newString = oldString.replace('Robbert', 'Tom');
console.log(newString);

Output:

Hello everyone, I am Tom!

Now let’s apply it to remove all the spaces from a string. If we do like this: 

var myString = 'Today is a bad day in Miami';
result = myString.replace(/ /,'');
console.log(result);

It will only delete one first space from the string.

Output

Todayis a bad day in Miami

So if we want to remove all of the space in our string, we have to set it globally:

result = myString.replace(/ /g,'');

Completed code: 

var myString = 'Today is a bad day in Miami';
var result = '';
result = myString.replace(/ /g,'');
console.log(result);

Output:

TodayisabaddayinMiami

If we want to match all whitespace, we can do the following: 

result = myString.replace(/\s/g,'');

The ‘\s’ means the spaces, new lines, and tabs. 

Method 3: Using the string.replaceAll() method

Syntax

string.replaceAll(pattern, replacement)

Parameters:

The replaceAll() method replaces all occurrences of pattern and replaces it with replacement.

In our case, we have to include the global flag set (/g). So basically, we will use the string.replaceAll() method just like the string.replace() method above. 

Completed code: 

var myString = 'Today is a bad day in Miami';
var result = '';
result = myString.replaceAll(/\s/g,'');
console.log(result);

Output:

TodayisabaddayinMiami

Summary

In this tutorial, we have guided you through the three most common methods to remove all spaces from a string in JavaScript. You can either use a for loop, a string.replace() method, or a string.replaceAll() method.

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *