How to remove spaces from a string using JavaScript?
Last Updated :
15 Mar, 2024
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:
Examples of remove spaces from a string using JavaScript
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);
OutputGeeksforGeeksPortal
2. Using the replace() method
We can use the replace()
 method with a regular expression to globally replace all space occurrences with an empty 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
3.Using the reduce() method
The reduce()
 method, combined with the spread operator, allows us to remove spaces from a string. It iterates through each character, excluding spaces, and accumulates the non-space characters:
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
4. Using the trim() method
The trim()
 method removes leading and trailing whitespace from a string. It’s useful when dealing with user input or data from external sources:
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";
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
Share your thoughts in the comments
Please Login to comment...