Open In App
Related Articles

How to remove spaces from a string using JavaScript ?

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

In this article, we will learn how to remove spaces from a string using JavaScript. We will be given a string and we need to remove all the space present in the string and return the result string.

Remove-spaces-from-a-string-in-JavaScript

How to remove spaces from a string using JavaScript?

These are the following methods by using we can remove spaces from a string using JavaScript:

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

  • The split() method is used to split a string into multiple sub-strings and return them in the form of an array.
  • 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. 

Example: In this example, we will use the split() and join() Methods.

Javascript

let originalText = "Geeks for Geeks Portal";
 
let removedSpacesText = originalText.split(" ").join("");
console.log(removedSpacesText);

                    

Output
GeeksforGeeksPortal

Method 2: Using the replace() method

  • In this approach, we will pass the 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. 

Example: In this example, we will use replace() method with regex.

Javascript

let originalText = "Geeks for Geeks Portal";
 
let removedSpacesText = originalText.replace(/ /g, "");
console.log(removedSpacesText);

                    

Output
GeeksforGeeksPortal

Method 3: Using reduce() method

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 a single value from the array. The function checks whether each character of the string is space or not if it is space doesn’t add a character to the accumulator and if it is not space then add a character to the accumulator. At the last accumulator return string doesn’t contains any space in it.

Example: In this example, we will use reduce() method with spread operator

Javascript

let originalText = "Geeks for Geeks Portal";
 
let removedSpacesText =
    [...originalText].reduce((accum, char) =>
        (char === " ") ? accum : accum + char, '');
console.log(removedSpacesText);

                    

Output
GeeksforGeeksPortal

Method 4: Using the trim() method

This method removes all the whitespace from the start and end of the string. It returns the new string.

Example: In this example, we will remove the space from the start and end of the string using the trim() method.

Javascript

const geek = '   Geeksforgeeks   ';
console.log(geek.trim());
// Expected output: "Geeksforgeeks";

                    

Output
Geeksforgeeks

Method 5: Using Lodash _.trim() Method

In this approach we are using _.trim() method which trim the given string and return the string havinh no space.

Example: This example is the implementation of the above-explained approach.

Javascript

// Defining Lodash variable
const _ = require('lodash');
 
let str = " Geeks-for-Geeks ";
 
// Using _.trim() method
console.log(_.trim(str));

                    

Output:

Geeks-for-Geeks


Last Updated : 30 Nov, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads