DEV Community

BJ Cantlupe
BJ Cantlupe

Posted on • Originally published at bjcant.dev on

JS Numeric Separator

Here’s a new ECMAScript feature that I am excited about: numeric separators. Currently in proposal stage 3, this readability feature will make it much easier for our eyes to parse numbers. If you have Chrome version 75 or above — find your version by typing chrome://version/ in your URL input — you can test out this feature in Chrome dev tools right now!

// try this in the console
let oneMillion = 1_000_000

The underscore can be place between any numeric values, including after a decimal.

let pi = 3.141_592_653

You may notice that the actual numeric value omits the separator. Don’t get too attached!

pi
//=> 3.141592653

This is especially useful for separating binary numbers.

let binary = 0b1101_1100_0000

It also works for hexadecimal

let abc = 0xA_B_C

BigInt

let frickinHugeInt = 1_000_000_000_000_000_000_000n

… and exponential notation!

let infinitePi = 3.145e1_000

Separators can be placed in unconventional places, too.

let kwazyInt = 43_8_83998_123_583_1_0

Not sure why you would want to do that 🤷‍.

There are some limitations to the separator. These are the things you can’t do:

Place underscores at the beginning or end of a number

// no
_1000

// nope
1000_

Place underscores adjacent to a non-numeric character

// nah
1_.348

// also nah
5.43_e2

Place underscores adjacent to each other

// nice try
1 __000__ 000

Remember, this feature is still in proposal phase and as of this writing is not available on browsers other than Chrome. For Node apps, you will need to be running on v12.5 or higher. If you want to use this on the web, your only viable option for now is to transpile your JS with Babel, unless of course you are confident that all of your users are on the latest Chrome 😜. Gotta keep ‘em separated!

Top comments (0)