number to comma separated javascript code example
Example 1: number with commas js
function numberWithCommas(x) {
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',');
}
Example 2: convert number to comma separated format javascript
function numberWithCommas(x) {
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
function numberWithCommasDecimal(x) {
return x.toString().replace(/\B(?<!\.\d*)(?=(\d{3})+(?!\d))/g, ",");
}
Example 3: javascript array to comma separated string
var colors = ["red", "blue", "green"];
var colorsCSV = colors.join(",");