When working with numbers in Javascript, you may need to format them to make them more readable.Â
You can convert a number value to a comma-separated string. Here are two approaches:
- using toLocaleString()
- using Regex
-Â Conclusion
 using toLocaleString()
The toLocalString()
 is a default built-in browser method of the Number
 object that returns the number (in string) representing the locale.
You can pass any locale inside the parantheses as a parameter.
 const number = 14500240
 const formatedNumber = number.toLocaleString("en-IN")
 console.log(formatedNumber)
Â
 using Regex
function numberWithCommas(num) {
  return num.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',');
}
const number = numberWithCommas(234234.555);
console.log(number);
 Conclusion
After reading this article, you'll be able to use either of these two techniques to format numbers in Javascript:
 - using toLocaleString()
 - using Regex
Thanks for reading!
Top comments (0)