How to Print a Number with Commas as Thousands Separators in JavaScript

Here are the three ways to print a number with commas as thousands separators in JavaScript:

  1. Using Intl.NumberFormat()
  2. Using toLocaleString()
  3. Using “replace()” method with regex

Method 1: Using the “Intl.NumberFormat()” method

let number = 1234567.89;

let formatted = new Intl.NumberFormat().format(number);

console.log(formatted);

Output

1,234,567.89

Method 2: Using toLocaleString()

let number = 1234567.89;

let formatted = number.toLocaleString();

console.log(formatted);

Output

1,234,567.89

Method 3: Using the replace method with regex

let number = "1234567.89";

let formatted = number.replace(/\B(?=(\d{3})+(?!\d))/g, ',');

console.log(formatted);

Output

1,234,567.89

That’s it.