Categories
JavaScript Answers

How to format numbers with commas with JavaScript?

Spread the love

Sometimes, we want to format numbers with commas with JavaScript.

In this article, we’ll look at how to format numbers with commas with JavaScript.

How to format numbers with commas with JavaScript?

To format numbers with commas with JavaScript, we can use the toLocaleString method.

For instance, we write:

const no = 3456;
const noStr = no.toLocaleString();
console.log(noStr)

to call toLocaleString to return a number string with digit groups separated by commas.

Therefore, noStr is '3,456'.

Conclusion

To format numbers with commas with JavaScript, we can use the toLocaleString method.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *