by @codeexamples

How to remove whitespaces from a string in JavaScript?

Home » javascript » How to remove whitespaces from a string in JavaScript?

To remove the white spaces between the characters from a string we need to use the replace() method in JavaScript.

Here is an example:

const string = "hello have some fun";

const remove = string.replace(/\s/g,"");

console.log(remove); // hellohavesomefun

In the above code, we have passed the regex /\s/g where:

  • \s is the regex for whitespace.
  • g is the global flag, meaning match all \s whitespaces.