We can parse a number string with commas thousand separators into a number by removing the commas, and then use the +
operator to do the conversion.
For instance, we can write:
const num = +'123,456,789'.replace(/,/g, '');
console.log(num)
We call replace
with /,/g
to match all commas and replace them all with empty strings.
And then we use the unary +
operator to convert the string without the commas to a number.
Therefore, num
is 123456789.