Underscores as number separators
JS Bits Bill
Posted on March 26, 2021
Making calculations with longer numbers? You can improve readability by using underscores (_
) in numeric primitives:
// Hard to read:
Math.floor(Math.random() * 1000000); // 337083
// Better
Math.floor(Math.random() * 1_000_000); // 337083
1_000_000 === 1000000 // true
The underscore doesn't have to be placed where a comma would go - it can be used anywhere in the number except at the very beginning or end. Just a small way to improve your code and prevent errors!
Check out more #JSBits at my blog, jsbits-yo.com. Or follow me on Twitter!
💖 💪 🙅 🚩
JS Bits Bill
Posted on March 26, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
javascript How I Created Vanilla Calendar Pro — A Lightweight and Flexible JavaScript Calendar with TypeScript
November 28, 2024