1

In Ruby you may say:

n = 10_000 

or

n = 10000 

They are identical. I think you can also do something like this in Perl. This is a very nifty thing, to make large numbers more human readable.

My question is really two parts:

  1. Is there a Javascript equivalent? Because I would definitely use it.

  2. What do you call this underscore character, syntactically? I bet an experienced programmer can field this pretty easily, but I'm having a heck of a time trying to use the internet for a reverse dictionary. Such information I think would be useful to me in learning future languages. And it's absolutely driving me crazy, that I don't know how to describe it.

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
sas08
  • 183
  • 7

2 Answers2

6

Numeric Separators are now a feature of Javascript. ()

So, yes, n = 10_000 is the same as n = 10000 (n = 1_0_0_0_0 works too...)

Numeric Separators also work with Binary Literals and Hex Literals. Relevant doc: https://github.com/tc39/proposal-numeric-separator

At the time of writing, browser support is not excellent though, see caniuse.

Node 12.8.1 and above supports them.

Félix Paradis
  • 5,165
  • 6
  • 40
  • 49
4

1) No, there is not. At least not the way ruby does it. If you're familiar with scientific notation/E notation, you can turn a large number like 1000000000000000 into something more friendly like 1E15

As a sidenote: There has been a long-going discussion for coffeescript to include something similar to the ruby syntax here

2) It is called an underscore and has no special name in its role in Ruby integer literals. The only thing special in Ruby (and in Perl, Frink, Caml and others) is that underscores in integer literals are ignored. This feature is very old and was first used in Ada to make integer literals more readable.

Beat Richartz
  • 9,474
  • 1
  • 33
  • 50
  • Note that numeric separators are now supported in Typescript (as of v2.7) and are at stage 3 as an EcmaScript proposal (so not yet adopted as an official part of the language, but likely to be in the future) and are coming soon to Chrome/V8: https://github.com/tc39/proposal-numeric-separator – Patrick Finnigan Jun 23 '20 at 16:34