How to Remove Spaces From a String using JavaScript? Last Updated : 14 Nov, 2024 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 Comment More infoAdvertise with us Next Article How to Remove Spaces From a String using JavaScript? sayantanm19 Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Questions Similar Reads How to remove HTML tags from a string using JavaScript ? Removing HTML tags from a string in JavaScript means stripping out the markup elements, leaving only the plain text content. This is typically done to sanitize user input or to extract readable text from HTML code, ensuring no unwanted tags remain in the string.HTML tags come in two forms: opening t 3 min read How to remove white spaces from a string using jQuery ? In this article, we will see how to remove the white spaces from string using jQuery. To remove the white spaces, we will use trim() method. The trim() method is used to remove the white spaces from the beginning and end of a string. Syntax: jQuery.trim( str ) Parameter: This method accepts a single 1 min read How to remove time from date using JavaScript ? Given a Date Object and the task is to remove the time portion from the object using JavaScript. JavaScript split() Method: This method is used to split a string into an array of substrings, and returns the new array. Syntax: string.split( separator, limit ) Parameters: separator: This parameter is 2 min read How to replace all dots in a string using JavaScript ? We will replace all dots in a string using JavaScript. There are multiple approaches to manipulating a string in JavaScript. Table of Content Using JavaScript replace() MethodUsing JavaScript Split() and Join() Method Using JavaSccript reduce() Method and spread operatorUsing JavaScript replaceAll() 4 min read How to Remove Spaces from a String in TypeScript ? TypeScript offers various inbuilt functions to remove spaces from a string. These functions can be used to remove spaces between characters or entire words. Below, we explore different approaches to remove spaces from a string in TypeScript.Table of ContentUsing split() and join() methodsUsing repla 4 min read JavaScript - Remove Text From a String Here are the different approaches to remove specific text from a string in JavaScript, starting from the most commonly used approachesUsing replace() Method - Best MethodThe replace() method is a direct and widely used method to remove specific text by replacing it with an empty string "". This meth 2 min read How to Remove HTML Tags from a String in JavaScript? Removing HTML tags from a string in JavaScript is a common task, especially when dealing with user-generated content or when parsing HTML data from external sources. HTML tags are typically enclosed in angle brackets and removing them involves extracting only the text content from a string while dis 2 min read JavaScript - How to Remove All Line Breaks From a String? Here are different ways to remove all line breakes from a string in JavaScript.1. Using JavaScript RegEx with replace() Method This method uses regular expressions to detect and replace newlines in the string. It is fed into replace function along with a string to replace with, which in our case is 3 min read How to remove all Non-ASCII characters from the string using JavaScript ? In this article, we are given a string containing some non-ASCII characters and the task is to remove all non-ASCII characters from the given string. Approaches to remove all Non-ASCII Characters from String: Table of Content Using ASCII values in JavaScript regExUsing Unicode in JavaScript regExUsi 3 min read How to Remove Backslash from JSON String in JavaScript? Removing backslash from JSON String is important because it ensures proper JSON formatting for data interchange and prevents syntax errors during parsing, leading to accurate data representation and processing in JavaScript applications. we will going to learn three different approaches to removing 2 min read Like