Categories
JavaScript Answers

How to remove whitespaces inside a string in JavaScript?

Spread the love

To remove whitespaces inside a string in JavaScript, we call the string replace method.

For instance, we write

const s = "hello world".replace(/\s/g, "");

to call "hello world".replace to replace all spaces with empty strings.

\s matches spaces.

And the g flag makes replace replace all spaces.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *