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