What is JavaScript NumberFormat Comma and its Usage?

Are you ready to unfold the secret of the JavaScript NumberFormat Comma and its Usage? Read on!

In this article, you’ll explore the power of JavaScript’s NumberFormat method and learn how to format numbers for different locales, adjust decimal places, and even format currencies.

Apart from that, you’ll also discover alternative methods for number formatting, including using toLocaleString(), regular expressions, and custom functions.

What is NumberFormat in JavaScript?

NumberFormat is a method of the built-in Intl object in JavaScript that is used to format a number into a specific format or locale.

This can be handy when you want to display numbers in a more readable way, or in a format familiar to users from different regions.

For example:

let SampleNumber = 102030405060.123;

// Create our number formatter.
let formatter = new Intl.NumberFormat('en-US'); ✅

console.log(formatter.format(SampleNumber ));

As you can see in our first example, we’re creating a new NumberFormat object with the locale set to “en-US.”

When we call format() with our number, it returns the number formatted with commas or the JavaScript NumberFormat Comma, separating the thousands – which is the standard way of displaying numbers in the United States.



Not only that but the NumberFormat is even more powerful than that! You can customize it to use different numbering systems, adjust the number of decimal places, and even format currencies.

Here’s an example:

let number = 102030405060.789;

// Create our number formatter.
let formatter = new Intl.NumberFormat('en-PH', { ✅
    style: 'currency',
    currency: 'PHP'


In our second example, we’re formatting the number in Philippine money, PHP. We’ve set the style option to “currency,” and the currency option to “PHP.”

The formatted output includes the currency symbol and rounds to two decimal places, as is standard in the Philippines.



So, that’s NumberFormat in a nutshell! It’s a versatile tool for formatting numbers in JavaScript.

Different Ways to Format Numbers with Commas in JavaScript

Aside from using the Intl.NumberFormat you can also use other methods such as:

Use the toLocaleString() method

You can format numbers with commas as thousands separators using the toLocaleString() method.

Here’s an example:

let SampleNumber = 102030405060.789;
let formatted = SampleNumber.toLocaleString('en-US'); ✅



Use a Regular Expression

You can also use the regular expression to format numbers with commas.

Here’s an example:

let SampleNumber = 14030405060.143;
let formatted = SampleNumber.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","); ✅

The regular expression /\B(?=(\d{3})+(?!\d))/g matches positions where a comma needs to be inserted.



Use Custom Function

Apart from using the two solutions above, you can also use the custom function.

Here’s an example:

function formatNumberWithCommas(x) {✅
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");

let SampleNumber = 24030405060.143;
let formatted = formatNumberWithCommas(SampleNumber);

As you can see, we’ve wrapped the previous solution in a function for easier reuse.




In conclusion, we have explored the JavaScript NumberFormat method, which is a powerful tool for formatting numbers according to different cultural or locale-specific conventions.

It allows for customization to handle different numbering systems, adjust the number of decimal places, and even format currencies.

Apart from NumberFormat, JavaScript also provides other methods for number formatting such as toLocaleString(), regular expressions, and custom functions.

These methods offer flexibility and control over how numbers are presented, making JavaScript a versatile language for handling numbers in web development.

We hope this article has provided you with enough information to understand the JavaScript NumberFormat comma.

If you want to explore more JavaScript topics, check out the following articles:

Thank you for reading Itsourcecoders 😊.

Leave a Comment