Categories
JavaScript Answers

How to Parse a String with a Comma Thousand Separator to a Number with JavaScript?

Spread the love

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.

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 *