print numbers with commas as thousand separators in JavaScript

JavaScript
4
function numberWithCommas(x) {
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
console.log(numberWithCommas(1000000));
🤖 Code Explanation
The code defines a function to add commas to a number. The function takes a number as a parameter and returns a string with commas in the appropriate places. For example, the number 1000000 would be returned as "1,000,000".

More problems solved in JavaScript




















