New Best Practice
Sign In

Use numeric separators for large numeric literals

Friday, February 21, 2020

Numeric separators provide improved readability of large numeric literals by creating a visual separation between the digit groups. The numeric separators proposal for ECMAScript is currently a stage 3 proposal, has been implemented in TypeScript 2.7, and is already available in Chrome, Firefox and Safari. Check out caniuse.com for the latest support of numeric separators in modern browsers.

Code Examples

const BIG = 1000000000   // it's a billion. no, ten million. maybe, 10 billion. ugh

const APR = 2699 // is that 26.99 percent or 2,699% ?!?

avoid: large numeric literals without separators

const BIG = 1_000_000_000   // ohh, it _is_ a billion!

const APR = 26_99 // phew, only 26.99%

do: use numeric separators

Instructions

  • Do:

    use numeric separators to provide visual separation in order to improve code readability

  • Avoid:

    large numeric literals without visual separators

Brian Love

Brian is a software engineer and Google Developer Expert in Web Technologies and Angular with a passion for learning, writing, speaking, teaching and mentoring. He regularly speaks at conferences and meetups around the country, and co-authored "Why Angular for the Enterprise" for O'Reilly. When not coding, Brian enjoys skiing, hiking, and being in the outdoors. Brian recently launched lookout.dev where you can find best practices and expert advice on topics ranging from TypeScript, Angular, React, Node.js and more.

Google Developers Expert

Discussions are healthy ❤️