Here are the three ways to print a number with commas as thousands separators in JavaScript:
- Using “Intl.NumberFormat()” method
- Using “toLocaleString()” method
- Using the “replace()” method with “regex”
Method 1: Using the “Intl.NumberFormat()” method
The Intl.NumberFormat() method is used to represent numbers in language-sensitive formatting. It represents the currency or percentages according to the locale specified.
The locales parameter of this object is used to specify the format of the number. The “en-US” locale is used to specify that the locale takes the format of the United States and the English language, where numbers are represented with a comma between the thousands.
Syntax
Intl.NumberFormat(locales, options)
Parameters
- “locales”: It refers to the specific format of the language.
- “options”: It corresponds to the object comprising the properties.
Example
let number = 1234567.89;
let formatted = new Intl.NumberFormat().format(number);
console.log(formatted);
Output
1,234,567.89
Method 2: Using the “toLocaleString” method
The toLocaleString() method in JavaScript is used to return a string with a language-sensitive representation of a number. The optional “locales” parameter is used to specify the format of the number.
Syntax
toLocaleString(locales, options)
Parameters
- “locales”: It refers to the language format that needs to be used.
- “options”: It is an object where the properties can be set.
Example
let number = 1234567.89;
let formatted = number.toLocaleString();
console.log(formatted);
Output
1,234,567.89
Method 3: Using the “replace” method with “regex”
The “replace()” method is used to search a specific string for the value and replaces it with the new value, and the “regex” pattern does a global search based on the condition as its parameter.
Syntax
string.replace(searchValue, newValue)
Parameters
- “searchValue”: It refers to the value that needs to be searched.
- “newValue”: It corresponds to the value that needs to be replaced.
Example
We will take a number as a string.
let number = "1234567.89";
let formatted = number.replace(/\B(?=(\d{3})+(?!\d))/g, ',');
console.log(formatted);
Output
1,234,567.89
That’s it.

Krunal Lathiya is a seasoned Computer Science expert with over eight years in the tech industry. He boasts deep knowledge in Data Science and Machine Learning. Versed in Python, JavaScript, PHP, R, and Golang. Skilled in frameworks like Angular and React and platforms such as Node.js. His expertise spans both front-end and back-end development. His proficiency in the Python language stands as a testament to his versatility and commitment to the craft.