Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript numeric separators?

Tags:

javascript

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?

like image 608
Lucio Paiva Avatar asked Nov 21 '19 01:11

Lucio Paiva


Video Answer


2 Answers

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.

like image 62
Lucio Paiva Avatar answered Oct 21 '22 12:10

Lucio Paiva


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.

like image 1
Long Nguyen Duc Avatar answered Oct 21 '22 11:10

Long Nguyen Duc