JavaScript Numeric Separators

By  on  

Writing good code is important -- writing code that's easily human readable is a next level skill. It's not often that APIs are introduced whose seemingly only useful function is making code more readable, but let me introduce you to JavaScript numeric separators: an API that lets developers use underscores to make numbers more readable!

Consider the following number in your code:

// No commas for numbers in JavaScript
const oneHundredThousand = 100000;

Large numbers have always been difficult to mentally parse for engineers, so using underscores to make code more readable is a total win:

const oneMillion = 1_000_000; // 1000000
const decimals = 1_000_00.01_02_03 // 100000.010203

The only real rule with numeric separators is that the number cannot start or end with an underscore.

Numeric separators within JavaScript seem like spec creators gifting developers a feature. As an engineer, I'll take all I can get. You never stop learning in the world of JavaScript!

Recent Features

Incredible Demos

  • By
    Submit Button Enabling

    "Enabling" you ask? Yes. We all know how to disable the submit upon form submission and the reasons for doing so, but what about re-enabling the submit button after an allotted amount of time. After all, what if the user presses the "stop"...

  • By
    CSS pointer-events

    The responsibilities taken on by CSS seems to be increasingly blurring with JavaScript. Consider the -webkit-touch-callout CSS property, which prevents iOS's link dialog menu when you tap and hold a clickable element. The pointer-events property is even more JavaScript-like, preventing: click actions from doing...

Discussion

    Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!