Skip to content
Related Articles

Related Articles

How to remove spaces from a string using JavaScript ?
  • Difficulty Level : Medium
  • Last Updated : 26 Nov, 2019

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:




<!DOCTYPE html>
<html>
  
<head>
    <title>
        How to remove spaces from
        a string using JavaScript?
    </title>
</head>
  
<body>
    <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>
</body>
  
</html>

Output:



  • Before clicking the button:
    split-join-before
  • After clicking the button:
    split-join-after

Method 2: Using replace() method with regex: The replace() method is used to replace a specified string with another string. It takes two parameters, first is the string to be replaced and the second parameter is the string replaced with. The second string can be given as 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:




<!DOCTYPE html>
<html>
  
<head>
    <title>
        How to remove spaces from
        a string using JavaScript?
    </title>
</head>
  
<body>
    <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";
          
            newText = 
                originalText.replace(/ /g, "");
          
            document.querySelector('.output').textContent
                    = newText;
        }
    </script>
</body>
  
</html>

Output:

  • Before clicking the button:
    replace-regex-before
  • After clicking the button:
    replace-regex-after
My Personal Notes arrow_drop_up
Recommended Articles
Page :