How to print a number with commas as thousands separators in JavaScript How to print a number with commas as thousands separators in JavaScript javascript javascript

How to print a number with commas as thousands separators in JavaScript


I used the idea from Kerry's answer, but simplified it since I was just looking for something simple for my specific purpose. Here is what I did:

function numberWithCommas(x) {    return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");}