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
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › String › trim
String.prototype.trim() - JavaScript | MDN
The trim() method of String values removes whitespace from both ends of this string and returns a new string, without modifying the original string.
Discussions

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
How to remove whitespace from beginning and end of text in JavaScript
I’m working on a JavaScript project and I need to clean up user input by removing extra spaces. I have strings that sometimes have spaces, tabs, or other whitespace characters at the beginning or end. For example, when users type in a form field, they might accidentally add spaces before ... More on community.latenode.com
🌐 community.latenode.com
0
0
August 26, 2025
Removing Whitespace from a String
Hi all, Im working on massaging a string that has a lot of whitespace. My thought is to use split on space and then join all the non-empty values. My issue is when I use split, nothing changes. Not even on my test examples. Looking for Ideas on how to remove whitespace or a way to get split ... More on community.make.com
🌐 community.make.com
6
0
December 14, 2022
How to remove spaces from String?
I want to combine two expressions to create a bucket in aws. The bucket name cannot have any spaces. How would I remove spaces from the strings? ex. Bucket Name: {{ $(‘Merge’).item.json.Client }}-{{ $(‘Merge’).item.json[‘Title of Project’] }} | OUTPUT would be: New York-Marketing ... More on community.n8n.io
🌐 community.n8n.io
1
0
June 27, 2025
🌐
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 - Progress: 100% replaceAll: 902 907 ops/s, ±1.30% | fastest replace: 811 102 ops/s, ±0.42% | 10.17% slower split-join: 519 097 ops/s, ±1.99% | slowest, 42.51% slower spread-filter-join: 624 614 ops/s, ±0.41% | 30.82% slower Finished 4 cases! Fastest: replaceAll Slowest: split-join Running "Remove spaces (1000 characters string)" suite...
🌐
W3Schools
w3schools.com › jsref › jsref_trim_string.asp
JavaScript String trim() Method
"; let result = text.trim(); Try it Yourself » · Remove spaces with replace() using a regular expression: let text = " Hello World! "; let result = text.replace(/^\s+|\s+$/gm,''); Try it Yourself » · The trim() method removes whitespace ...
🌐
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 - It handles various types of whitespace including regular spaces, tabs (\t), and line breaks (\n, \r). When you only need to remove whitespace from the beginning of a string, use trimStart().
🌐
DEV Community
dev.to › natclark › removing-whitespace-from-strings-in-javascript-4k32
Removing Whitespace From Strings in JavaScript - DEV Community
September 7, 2021 - If you just want to remove the space character and not all whitespace, this snippet will do the trick: const string = `This is an example string.`; string.replace(/ /g, ``); Keep in mind, it won't remove consecutive spaces or tabs. For example, "Example string" will become "Examplestring". However, "Example string" (with two spaces) will become "Example string" (with one space).
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-remove-spaces-from-a-string-using-javascript
How to Remove Spaces From a String using JavaScript? - GeeksforGeeks
JavaScript string.replaceAll() method replaces all occurrences of a substring from a string. It will replace all the whitespaces with an empty string. ... The JavaScript string.trim() method removes leading and trailing whitespace from a string.
Published   July 12, 2025
🌐
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
🌐
p5.js
p5js.org › reference
Reference
Find easy explanations for every piece of p5.js code.
🌐
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 - By the end of this article, you ... includes various whitespace characters. Use the regular expression /\s+/g in combination with the replace() method to remove all forms of whitespace....
🌐
Latenode
community.latenode.com › other questions › javascript
How to remove whitespace from beginning and end of text in JavaScript - JavaScript - Latenode Official Community
August 26, 2025 - I’m working on a JavaScript project and I need to clean up user input by removing extra spaces. I have strings that sometimes have spaces, tabs, or other whitespace characters at the beginning or end. For example, when u…
🌐
Make Community
community.make.com › questions
Removing Whitespace from a String - Questions - Make Community
December 14, 2022 - Hi all, Im working on massaging a string that has a lot of whitespace. My thought is to use split on space and then join all the non-empty values. My issue is when I use split, nothing changes. Not even on my test exampl…
🌐
Obfuscator
obfuscator.io
JavaScript obfuscator tool - Protect Your JS Code
There are numerous reasons to protect your code: prevent anyone from simply copy/pasting your work (especially important for client-side projects like HTML5 games), remove comments and whitespace to make code faster to load and harder to understand, and protect work that hasn't been paid for yet so you can show clients without giving them the source code. What is VM obfuscation and how is it different from standard obfuscation? VM (Virtual Machine) obfuscation transforms your JavaScript code into custom bytecode that runs on an embedded interpreter.
🌐
Coding Beauty
codingbeautydev.com › home › posts › how to remove all whitespace from a string in javascript
How to Remove All Whitespace from a String in JavaScript - Coding Beauty
June 20, 2022 - To remove all whitespace from a string in JavaScript, call the replace() method on the string, passing a regular expression that matches any whitespace character, and an empty string as a replacement.
🌐
Medium
tariibaba.medium.com › javascript-remove-all-spaces-from-string-75f942ef2fbe
How to Remove All Spaces from a String in JavaScript | Medium
June 22, 2022 - The regular expression we specified only matches spaces in the string. To match and remove all whitespace characters (spaces, tabs and newlines), we’ll have to use a different regex: const str = 'A B C \t D \n E'; const whitespaceRemoved = str.replace(/\s/g, '');console.log(whitespaceRemoved); // ABC ... A captivating guide to the subtle caveats and lesser-known parts of JavaScript.
🌐
Go Make Things
gomakethings.com › articles › how-to-trim-whitespace-from-the-beginning-and-end-of-a-string-with-vanilla-js
How to trim whitespace from the beginning and end of a string with vanilla JS | Go Make Things
How would you remove that unneeded whitespace? JavaScript provides three different methods, depending on what you’re trying to do. The String.trim() method removes leading and trailing whitespace from a string.
🌐
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
🌐
n8n
community.n8n.io › questions
How to remove spaces from String? - Questions - n8n Community
June 27, 2025 - I want to combine two expressions to create a bucket in aws. The bucket name cannot have any spaces. How would I remove spaces from the strings? ex. Bucket Name: {{ $(‘Merge’).item.json.Client }}-{{ $(‘Merge’).item.json[‘Title of Project’] }} | OUTPUT would be: New York-Marketing Campaign OUTPUT should be: NewYork-MarketingCampaign
🌐
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
🌐
UiPath Community
forum.uipath.com › help › studio
String.Trim is not able to remove spaces between two words - Studio - UiPath Community Forum
October 1, 2023 - Hello , im facing a wierd situation here , the String.Trim function is not able to remove spaces in between of words here is the string for eg “Pdl Delinquncy file” After the trim it should be “PdlDelinquencyfile” B…