Let’s see how you can format number with commas in JavaScript
Sometimes, you may need to format a number value with commas in your HTML pages to make it easier to read. You can transform a number value into a comma-separated string by using JavaScript. Here are several ways to do that.
The Number.toLocaleString()
method is a built-in method of the Number
object that returns a locale-sensitive string representing the number. You can pass "en-US"
as the parameter so that it will always create a thousand separator for your number.
Here’s an example:
let n = 234234234;
let str = n.toLocaleString("en-US");
console.log(str); // "234,234,234"
#javascript