xxxxxxxxxx
const str = "This is a string with spaces";
const newStr = str.replace(/ /g, "");
console.log(newStr);
xxxxxxxxxx
// replaces spaces with '_'
str = str.replace(/ /g, "_");
// or
str = str.split(' ').join('_');
xxxxxxxxxx
var string = "Javascript is fun";
var newString = string.replace(" ", "_");
console.log(newString); // Javascript_is_fun