Numeric Literal Separators: 2 Minute Standards
Part of #StandardsIn2Min, an effort to provide short, but useful information about standards. Follow @StandardsIn2Min for a low-stress way to keep up, 2 minutes at a time.
Imagine if we didn't have spaces...
var x = 10000000;
This is, basically, the same problem. How many is that? It's too hard for our eyeballs to parse. If we were to display that in any form intended to be readable, we'd use some kind of separators to put these into hundred, thousands, millions, etc.. Like this...
Numeric separators allow us to achieve this same visual separation for readability in code, using the
_ (underscore) character. The same example above could be written now as..
var x = 10_000_000;
That is, obviously, an improvement for readability.
10_000.00), binary (using 'b', like
0b1101_0010), octal (using 'o' like
0o7_6_5) and hexidecimal (using '0x' like
0xA0_B2_C3), and BigInt (using 'n' like
There are just a few limitations to be aware of..
- You cannot use two contiguous separators
- You cannot end a number with a separator
- You cannot begin a number with a leading 0 immediately followed by a separator
That's about all there is to it. It has rich support in both modern, shipping engines and is also usable by transpilers.