JavaScript: Format Number With Commas

This javascript function will allow you to format a number with commas. This is great for formatting long numbers and prices, such as $10,000 or 10m. The javascript code below can be used in javascript files or on your website’s HTML page:

How to JavaScript Format Number With Commas?

This is a great javascript function when you need to format numbers with commas. This function can be used in javascript files or in the HTML tag on your website: This is great for formatting numbers such as $999,000.

The javascript code below can be used in javascript files or on your website’s HTML page:

var number= ","; 

va newStr = number.toLocaleString("en-US");

 return newStr;

There are several methods for javascript format number with commas.


The Number.toLocaleString() method is a built-in method of the Number object that returns an engaging locale-sensitive string representing your number! You can use “en-US” as the parameter so you know it will always create a thousand separators for your number and be sure to get accurate results no matter where in North America you are located or what language settings on your device, which is great because we live in such diverse communities these days don’t ya think?




let number = 234234234;
let str = number.toLocaleString("en-US");
console.log(str); // "234,234,234"

In the above code example, we are using the javascript format number with commas to create a thousand separator.

Note: You can also try this function on float value.

let number = 234234.555;
let str = number.toLocaleString("en-US");
console.log(str); // "234,234.555"

This formatting is also done by using String.replace() and regular expression.

Regular expression (regex):

Regular expressions are a type of pattern used to match the text. You can use javascript regular expression in search fields, such as the Google search engine or other popular websites like Facebook and Twitter.

Regular expressions are a powerful tool that you can use to find and replace values in your string. For example, let’s say we want to do the following:



Replace every space with an underscore so it has no spaces at all; Remove any trailing whitespace characters from our document after saving or before opening (lots of people forget this step); Make sure line breaks stay as they should be without having anything else get swapped around them. Let’s look at how these regular expression patterns would work out for different strings below!


function numberWithCommas(num) {
  return num.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',');

let n = numberWithCommas(234234.555);
console.log(n); // "234,234.555"

In the above code, we use regular expressions for javascript format numbers with commas to create a thousand separators.

String.replace(): javascript format number with commas

The String object has a replace() method, which is used to find and swap out all occurrences of one substring within another string! The parameter, in this case, would be the regex pattern you want to use for your javascript format number with commas.


The Intl.NumberFormat is a javascript method that will allow you to easily create the desired formatting for your numbers! This javascript function can be used on its own or combined with other javascript functions.

const givenNumber = 123423134231233423;

internationalNumberFormat = new Intl.NumberFormat('en-US')

These are the methods of formatting javascript numbers with commas. You can use the String object’s replace() method for regular expressions, Intl.NumberFormat to create the desired formatting for your numbers or combine with other javascript functions such as Number.toLocaleString().


If you’ve been stuck trying to format a number with commas, we hope this article has helped. We know how difficult it can be when there are so many ways to accomplish the same thing in JavaScript and even more confusion around which is best for your particular situation. Now that you’re armed with our three favorite methods for formatting numbers as well as some additional resources on the subject, hopefully, you feel better prepared to tackle any future tasks involving JavaScript and text manipulation!

Also Read: JavaScript Integer Division

Follow us on Facebook

Leave a Comment