Some languages, like Java and C++, allow you to use numeric separators. To improve readability, you can group digits per thousand:
1_000_000_000
But not only per thousand; you can use it anywhere really. Say you have some value in centimeters. Separating it like this makes it easier for humans to read it as meters:
10_00 // 10 meters
Wouldn't it be great if we could have this in JavaScript as well?
Guess what? It is becoming a thing now :-)
And it works for any numeric base:
const decimal = 1_234;
const binary = 0b1000_0101;
const hex = 0x12_34_56_78;
As of May 2020, it is supported by all major browsers (Chrome, Firefox, Edge, Safari, Opera) (source 1, source 2). And if you're working on the server side, Node.js v12.5.0 is already supporting it as well. Oh, and Electron too.
Interesting note: although supported by all browsers, this is not part of any ECMAScript version yet. It is still a stage 3 proposal (though when it gets to 4 it's basically ready to be released). Good to see that browsers are quickly catching up with new proposals.
It sounds interesting. I found this worked on the latest version of Chrome.
Babel also supports it in stage-0
. Then you can use Babel to transpile to ECMAScript 2015 (ES6).
Try it on Babel.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With