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.

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:
- 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);
|
OutputGeeksforGeeksPortal
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);
|
OutputGeeksforGeeksPortal
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);
|
OutputGeeksforGeeksPortal
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());
|
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
const _ = require('lodash');
let str = " Geeks-for-Geeks ";
console.log(_.trim(str));
|
Output:
Geeks-for-Geeks
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!
Last Updated :
30 Nov, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...