How to Remove Spaces From a String using JavaScript? Last Updated : 12 Jul, 2025 Comments Improve Suggest changes Like Article Like Report These are the following ways to remove space from the given string:1. Using string.split() and array.join() MethodsJavaScript string.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. JavaScript let s = " Geeks for Geeks "; let res = s.split(" ").join(""); console.log(res); OutputGeeksforGeeks 2. Using string.replaceAll() MethodJavaScript string.replaceAll() method replaces all occurrences of a substring from a string. It will replace all the whitespaces with an empty string. JavaScript let s = " Geeks for Geeks "; let res = s.replaceAll(" ", ""); console.log(res); OutputGeeksforGeeks 3. Using string.trim() MethodThe JavaScript string.trim() method removes leading and trailing whitespace from a string. JavaScript let s = " Geeks for Geeks "; let res = s.trim(); console.log(res); OutputGeeks for Geeks Note: The trim() method only removes the leading and trailing whitespaces from the string.4. Using Lodash _.trim() MethodThe _.trim() method method removes leading and trailing whitespace from a string. JavaScript const _ = require('lodash'); let s = " Geeks for Geeks "; let res = _.trim(s); console.log(res); OutputGeeks for Geeks5. Using Regular Expressions with string.replace() MethodJavaScript string.replace() method is used to replace a substring. With Regular expressions pattern we can find all spaces (/\s/g) and globally replace all space occurrences with an empty string. JavaScript let s = " Geeks for Geeks "; let res = s.replace(/\s+/g, ""); console.log(res); OutputGeeksforGeeks 6. Using string.match() with array.join() MethodJavaScript string.match() method returns an array of strings matching the regex pattern given as argument. and array.join() method is used to covert the array of strings to a single string. JavaScript let s = " Geeks for Geeks "; let res = s.match(/\S+/g).join(""); console.log(res); OutputGeeksforGeeks How to remove spaces from a string using JavaScript? Comment More info S sayantanm19 Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Questions Explore JavaScript BasicsIntroduction to JavaScript4 min readVariables and Datatypes in JavaScript6 min readJavaScript Operators5 min readControl Statements in JavaScript4 min readArray & StringJavaScript Arrays7 min readJavaScript Array Methods7 min readJavaScript Strings5 min readJavaScript String Methods9 min readFunction & ObjectFunctions in JavaScript5 min readJavaScript Function Expression3 min readFunction Overloading in JavaScript4 min readObjects in JavaScript4 min readJavaScript Object Constructors4 min readOOPObject Oriented Programming in JavaScript3 min readClasses and Objects in JavaScript4 min readWhat Are Access Modifiers In JavaScript ?5 min readJavaScript Constructor Method7 min readAsynchronous JavaScriptAsynchronous JavaScript2 min readJavaScript Callbacks4 min readJavaScript Promise4 min readEvent Loop in JavaScript4 min readAsync and Await in JavaScript2 min readException HandlingJavascript Error and Exceptional Handling6 min readJavaScript Errors Throw and Try to Catch2 min readHow to create custom errors in JavaScript ?2 min readJavaScript TypeError - Invalid Array.prototype.sort argument1 min readDOMHTML DOM (Document Object Model)9 min readHow to select DOM Elements in JavaScript ?3 min readJavaScript Custom Events4 min readJavaScript addEventListener() with Examples9 min readAdvanced TopicsClosure in JavaScript4 min readJavaScript Hoisting6 min readScope of Variables in JavaScript3 min readJavaScript Higher Order Functions7 min readDebugging in JavaScript4 min read Like