How to remove spaces from a string in JavaScript?

To remove spaces from a string in JavaScript, do this: string.replaceAll(' ', '').

Here's how you do it:

const string = ' Hello World ! ';
const stringNoSpaces = string.replaceAll(' ', '');

console.log(stringNoSpaces); // HelloWorld!