How to remove spaces from a string using JavaScript ?

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!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>

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!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>

chevron_right


Output:

  • Before clicking the button:
    replace-regex-before
  • After clicking the button:
    replace-regex-after

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.