How to remove white spaces from a entire string JavaScript

by Sai gowtham  1 Min read

Consider we have a string with a lot of white space.

const str = 'this  is  a  big  old  string';

there is a replace method in the JavaScript which helps us to remove the white spaces in the entire string.

Replace method accepts two arguments.

const str = 'this  is  a  big  old  string';


str.replace(/\s/gi , '')

//"thisisabigoldstring"

The first argument is a regular expression which helps us to scan over the entire string and remove the white space.

Second way

const str = 'this  is  a  big  old  string';


str.replace(/\W/gi , '')

//"thisisabigoldstring"

Happy coding…