For space-character removal use

"hello world".replace(/\s/g, "");

for all white space use the suggestion by Rocket in the comments below!

Answer from Henrik Andersson on Stack Overflow
🌐
Career Karma
careerkarma.com › blog › javascript › a guide to remove spaces from string in javascript
Remove Spaces From String JavaScript | Career Karma
September 24, 2020 - <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width"> <style> body { font-family: 'Roboto', Courier, monospace } </style> <script> const handleSubmit = str => { //loop let newStr = ""; for(let i = 0; i < str.length; i++) { if(str[i] !== " ") { newStr += str[i]; } } document.getElementById("demo").innerHTML = newStr; } </script> </head> <body> <h1>Whitespace Filter</h1> <input id="demoString" value="" type="text" name="inputString" placeholder="I'm a string"/> <input type="submit" onclick="handleSubmit(demoString.value)"/> <div id="demo"></div> </body> </html>
Discussions

Ways to remove spaces from a string using JavaScript
Thanks for sharing. I just did a performance test. The average time it takes each function (10,000 runs) to remove the spaces from a text with 1800 words/spaces in it. replaceAll: 0.08585000002384185 miliseconds replace: 0.10449000005722046 miliseconds splitAndJoin: 0.2322219943579563 miliseconds filterAndJoin: 1.1504000001549721 miliseconds Seems like replaceAll is the fastest and filterAndJoin is the slowest. More on reddit.com
🌐 r/learnjavascript
33
204
January 11, 2023
text - How to remove spaces from a string using JavaScript? - Stack Overflow
How to remove spaces in a string? For instance: Input: '/var/www/site/Brand new document.docx' Output: '/var/www/site/Brandnewdocument.docx' More on stackoverflow.com
🌐 stackoverflow.com
Remove whitespace Best Practice !?
hi everyone if you could rate my solution for that challenge, could you consider it Best Practice or Less or More (Average) or Newbie?? here my solution: let hello = " Hello, World! "; let wsRegex = /\s/g; // Chan… More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
5
0
May 13, 2019
javascript - Remove ALL white spaces from text - Stack Overflow
The g character makes it a "global" match, meaning it repeats the search through the entire string. Read about this, and other RegEx modifiers available in JavaScript here. If you want to match all whitespace, and not just the literal space character, use \s instead: More on stackoverflow.com
🌐 stackoverflow.com
🌐
Reddit
reddit.com › r/learnjavascript › ways to remove spaces from a string using javascript
r/learnjavascript on Reddit: Ways to remove spaces from a string using JavaScript
January 11, 2023 - 204 votes, 33 comments. 290K subscribers in the learnjavascript community. This subreddit is for anyone who wants to learn JavaScript or help others do so. Questions and posts about frontend development in general are welcome, as are all posts pertaining to JavaScript on the backend.
🌐
Vultr Docs
docs.vultr.com › javascript › examples › remove-all-whitespaces-from-a-text
JavaScript Program to Remove All Whitespaces From a Text | Vultr Docs
December 18, 2024 - Note that this method only removes spaces and not other types of whitespace like tabs or newlines. Initialize an empty string to store the result. Loop through each character of the original string.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › String › trim
String.prototype.trim() - JavaScript - MDN Web Docs
The trim() method of String values removes whitespace from both ends of this string and returns a new string, without modifying the original string.
Find elsewhere
🌐
W3Schools
w3schools.com › jsref › jsref_trim_string.asp
JavaScript String trim() Method
The trim() method removes whitespace from both sides of a string. The trim() method does not change the original string. ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: ...
🌐
freeCodeCamp
forum.freecodecamp.org › curriculum help
Remove whitespace Best Practice !? - Curriculum Help - The freeCodeCamp Forum
May 13, 2019 - hi everyone if you could rate my solution for that challenge, could you consider it Best Practice or Less or More (Average) or Newbie?? here my solution: let hello = " Hello, World! "; let wsRegex = /\s/g; // Change this line let result = hello.replace(wsRegex, "").replace(/,/g, ", "); // Change this
🌐
Sololearn
sololearn.com › en › Discuss › 2734075 › how-to-remove-all-whitespaces-and-line-breaks-in-js-
How to remove all whitespaces and line breaks in js ?
March 23, 2021 - sl_scroll_/de/Discuss/359554/salam-les-amis-jai-un-probleme-en-javascript-est-ce-que-vous-pouvez-me-donnez-des-sites-ou-des-cours-pour-b1-dcouvrir-ce-languPending
🌐
JavaScript in Plain English
javascript.plainenglish.io › javascript-remove-all-whitespace-from-string-ece685d0ec33
How to Remove All Whitespace from a String in JavaScript | JavaScript in Plain English
July 22, 2024 - For example, str.replace(/\s/g, '') returns a new string with all whitespace removed from str. const str = '1 2 3';const whitespaceRemoved = str.replace(/\s/g, ''); console.log(whitespaceRemoved); // 123 · The \s regex metacharacter matches whitespace characters, such as spaces, tabs, and newlines. Join Medium for free to get updates from this writer. ... We use the g regex flag to specify that all whitespace characters in the string should be matched.
🌐
Quora
quora.com › How-do-I-remove-all-the-whitespaces-from-string-in-Javascript
How to remove all the whitespaces from string in Javascript - Quora
Quora is a place to gain and share knowledge. It's a platform to ask questions and connect with people who contribute unique insights and quality answers.
🌐
Bobby Hadz
bobbyhadz.com › blog › javascript-remove-all-whitespace-from-string
Remove/Replace all Whitespace from a String in JavaScript | bobbyhadz
Last updated: Mar 1, 2024 Reading time·6 min · Use the String.replace() method to remove all whitespace from a string, e.g. str.replace(/\s/g, '')
🌐
Techie Delight
techiedelight.com › home › java › remove all whitespace from a string in javascript
Remove all whitespace from a string in JavaScript | Techie Delight
3 weeks ago - The g switch in the regular expression performs the search and replace throughout the entire string. ... To remove all whitespace characters from the string, and not just the space character, use \s instead.
🌐
Futurestud.io
futurestud.io › tutorials › remove-all-whitespace-from-a-string-in-javascript
Remove All Whitespace From a String in JavaScript
const stripped = ' My String With A Lot Whitespace '.replace(/\s+/g, '') // 'MyStringWithALotWhitespace' Let’s look at the individual parts of the Regex and determine what they do: \s: matches any whitespace symbol: spaces, tabs, and line breaks
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-remove-spaces-from-a-string-using-javascript
How to Remove Spaces From a String using JavaScript? - GeeksforGeeks
The JavaScript string.trim() method removes leading and trailing whitespace from a string.
Published   July 12, 2025
🌐
CoreUI
coreui.io › answers › how-to-trim-whitespace-from-a-string-in-javascript
How to trim whitespace from a string in JavaScript · CoreUI
May 19, 2026 - Use the trim() method to remove leading and trailing whitespace from strings in JavaScript efficiently.
🌐
TechOnTheNet
techonthenet.com › js › string_trim.php
JavaScript: String trim() method
Let's take a look at an example of how to use the trim() method in JavaScript. ... In this example, we have declared a variable called totn_string that is assigned the string value of 'TechOnTheNet'. We have then invoked the trim() method of the totn_string variable to remove the whitespace ...