Use Separators to Make Your Number Literals More Readable
Let's imagine you have a really big number like this
let billion = 1000000000;
This seems to be quite difficult to read. Since this is a number and not a string, you can't simply add commas to make it more readable.
let billion = 1,000,000,000; //This will give you an error
The good news is that most modern browsers and Node.js support numeric separators, which are underscores that can be placed within numeric literals to break them up into more manageable chunks. Using numeric separators, you can rewrite it as
let billion = 1_000_000_000;
Numeric separators can even be used directly within functions and operators. For example, you can still do things like
let billionInverse = 1 / 1_000_000_000; //1e-9
Get my free, weekly JavaScript tutorials
Want to improve your JavaScript fluency?
Every week, I send a new full-length JavaScript article to thousands of developers. Learn about asynchronous programming, closures, and best practices — as well as general tips for software engineers.
Join today, and level up your JavaScript every Sunday!
Thank you, Taha, for your amazing newsletter. I’m really benefiting from the valuable insights and tips you share.