Why .replace method doesn't work this way?

Viewed 30

I want to clear all the whitespaces in the string arrayIsNowJoined, and everywhere I look up target the solution I have...but it just doesn't work. I got the following code:

const array1 = ["1, 3, 4, 7, 16", "1, 2, 4, 16"];
let arrayIsNowJoined = array1.join();

arrayIsNowJoined.replace(/\s/g, "");
console.log(arrayIsNowJoined);

What I'm doing wrong?

2 Answers

You must assign variable after replace

arrayIsNowJoined = arrayIsNowJoined.replace(/\s/g, "");

You need an assignment of the result, because strings are immutable.

const array1 = ["1, 3, 4, 7, 16", "1, 2, 4, 16"];
let arrayIsNowJoined = array1.join();

arrayIsNowJoined = arrayIsNowJoined.replace(/\s/g, "");
console.log(arrayIsNowJoined);

Related