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!
let oneMillion = 1_000_000
The underscore can be place between any numeric values, including after a decimal.
You may notice that the actual numeric value omits the separator. Don’t get too attached!
This is especially useful for separating binary numbers.
let binary = 0b1101_1100_0000
It also works for hexadecimal…
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
Place underscores adjacent to a non-numeric character
Place underscores adjacent to each other
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!