How to add commas to a number in JavaScript

I was working on something where I wanted to add commas to a number that didn’t have commas. I like things to be readable ya know? Turns out it’s really easy. All you need to do is set up the “toLocaleString” method and JavaScript does the heavy lifting. After reading the MDN docs on it, it appears that the method is used mostly in dates but I think it works for this use case as well. Here is a codepen for it. Just hit the button and you’ll see how I set number to 50000000 but the output will be 50,000,000.

See the Pen Number Formatting by Andrew O’Connor (@AndrewOnTheWeb) on CodePen.