how to format an integer with a comma in javascript

JavaScript
function numberWithCommas(x) {
  return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',');
}x.toString().toLocaleString(); // x must be a number
Source

Also in JavaScript: