Skip to content
Related Articles
Open in App
Not now

Related Articles

How to remove spaces from a string using JavaScript ?

Improve Article
Save Article
Like Article
  • Difficulty Level : Medium
  • Last Updated : 23 Jan, 2023
Improve Article
Save Article
Like Article

Method 1: Using split() and join() Method.

The split() method is used to split a string into multiple sub-strings and return them in the form of an array. A separator can be specified as a parameter so that the string is split whenever that separator is found in the string. The space character (” “) is specified in this parameter to separate the string whenever a space occurs. The join() method is used to join an array of strings using a separator. This will return a new string with the joined string using the specified separator. This method is used on the returned array and no separator (“”) is used to join the strings. This will join the strings in the array and return a new string. This will remove all the spaces in the original string. 

Syntax:

string.split(" ").join("")

Example: 

html




<h1 style="color: green">
    GeeksforGeeks
</h1>
<b>
    How to remove spaces from
    a string using JavaScript?
</b>
<p>
    Original string is:
    Geeks for Geeks Portal
</p>
<p>
    New String is:
    <span class="output"></span>
</p>
<button onclick="removeSpaces()">
    Remove Spaces
</button>
<script type="text/javascript">
    function removeSpaces() {
        originalText =
            "Geeks for Geeks Portal";
      
        removedSpacesText =
            originalText.split(" ").join("");
      
        document.querySelector('.output').textContent
                = removedSpacesText;
    }
</script>

Output:

How to remove spaces from a string using JavaScript ?

How to remove spaces from a string using JavaScript ?

Method 2: Using replace() method with regex.

The replace() method is used to replace a specified string with another string. It takes two parameters, the first is the string to be replaced and the second parameter is the string replaced with. The second string can be given as an empty string so that the empty space to be replaced. The first parameter is given a regular expression with a space character (” “) along with the global property. This will select every occurrence of space in the string and it can then be removed by using an empty string in the second parameter. This will remove all the spaces in the original string. 

Syntax:

string.replace(/ /g, "")

Example: 

html




<h1 style="color: green">
    GeeksforGeeks
</h1>
<b>
    How to remove spaces from
    a string using JavaScript?
</b>
<p>
    Original string is:
    Geeks for Geeks Portal
</p>
<p>
    New String is:
    <span class="output"></span>
</p>
<button onclick="removeSpaces()">
    Remove Spaces
</button>
<script>
    function removeSpaces() {
        originalText =
            "Geeks for Geeks Portal";
      
        newText =
            originalText.replace(/ /g, "");
      
        document.querySelector('.output').textContent
                = newText;
    }
</script>

Output:

How to remove spaces from a string using JavaScript ?

How to remove spaces from a string using JavaScript ?

Method 3: Using reduce() method with spread operator.

The spread operator is used to convert the string to an array and The reduce() method is used to reduce the array to a single value and executes a provided function for each value of the array and the return value of the function is stored in an accumulator and form an single value from the array. Function check each character of string is space or not if it is space don’t add character to accumulator and if it not space then add character to accumulator. At the last accumulator return string which doesn’t contains any space in it.

Syntax: 

[...string].reduce((accum, char)=> (char===" ") ? accum : accum + char, '')

Example3: 

HTML




<h1 style="color: green">
    GeeksforGeeks
</h1>
<b>
    How to remove spaces from
    a string using JavaScript?
</b>
<p>
    Original string is:
    Geeks for Geeks Portal
</p>
<p>
    New String is:
    <span class="output"></span>
</p>
<button onclick="removeSpaces()">
    Remove Spaces
</button>
  
<script>
    function removeSpaces() {
        originalText =
            "Geeks for Geeks Portal";
      
        removedSpacesText = [...originalText].reduce((accum, char)=> (char===" ") ? accum : accum+char, '') ;
      
        document.querySelector('.output').textContent
                = removedSpacesText;
    }
</script>

Output:

How to remove spaces from a string using JavaScript ?

How to remove spaces from a string using JavaScript ?


My Personal Notes arrow_drop_up
Like Article
Save Article
Related Articles

Start Your Coding Journey Now!