Write code to strip whitespaces from String in JavaScript?

devquora
devquora

Posted On: Jan 12, 2021

 

str.trim() method is used to remove the white spaces from both the ends of the given string.

Syntax:

str.trim()

Return value: This method returns a new string, without any of the leading or the trailing white spaces.

You can use the following code to strip whitespaces from String in JavaScript:

<!DOCTYPE html> 

<html> 

  

<head> 

    <title> 

        How to remove spaces from 

        a string using JavaScript? 

    </title> 

</head> 

  

<body> 

    <h1 style="color: green"> 

        Online Interview Questions 

    </h1> 

      

    <b> 

        How to remove spaces from 

        a string using JavaScript? 

    </b> 

      

    <p> 

        Original string is:  

        Online Interviews Questions 

    </p> 

      

    <p> 

        New String is:  

        <span class="output"></span> 

    </p> 

      

    <button onclick="removeSpaces()"> 

        Remove Spaces 

    </button> 

      

    <script type="text/javascript"> 

        function removeSpaces() { 

            originalText =  

                "Online Interviews Questions"; 

          

            newText =  

                originalText.replace(/ /g, ""); 

          

            document.querySelector('.output').textContent 

                    = newText; 

        } 

    </script> 

</body> 

  

</html>

Output:

Strip Whitespaces
Caption
 
 
  
 
    

    Related Questions

    Please Login or Register to leave a response.

    Related Questions

    JavaScript Tricky Interview Questions

    What is Coercion in JavaScript?

    Type coercion is the process of converting value from one type to another (such as string to number, object to boolean, and so on)...

    JavaScript Tricky Interview Questions

    What is Object Equality in Javascript?

    In JavaScript, Object Equality is used for two purposes basically. "==" is used for comparing two variables by ignoring its data type whereas "===" is used for comparing two variables by checking its ...