72

 

Why does calling 152..toString(2) return a binary string value of "10011000", when a call to 152.toString(2) throws the following exception?

          "SyntaxError: identifier starts immediately after numeric literal"


It seems to me that it's intuitive to want to use the latter call to toString(), as it looks & feels correct. The first example just seems plain odd to me.

Does anyone know why JavaScript was designed to behave like this?


15
  • 9
    Probably the parser tries to interpret 10.[andsomething] as a float number... just saying
    – SJuan76
    Oct 30, 2012 at 23:54
  • Because when you want to cast a literal number to a string you just just use a literal string ("10"). AKA, who cares.
    – Petah
    Oct 30, 2012 at 23:55
  • 3
    @SJuan76 Indeed. Enclosing the 10 in parens will work (10).toString() Oct 30, 2012 at 23:55
  • 2
    This is only a guess, but the parser probably thinks toString in 10.toString is the start of a fraction, while in the later case you have 0 as fraction and then run toString on the float. This would probably mean that var n = 10.; would be valid as well (never tried though).
    – Alxandr
    Oct 30, 2012 at 23:55
  • 3
    10.0.toString() is also works
    – neoascetic
    Oct 30, 2012 at 23:56

3 Answers 3

104

A . after a number might seem ambiguous. Is it a decimal or an object member operator?

However, the interpreter decides that it's a decimal, so you're missing the member operator.

It sees it as this:

(10.)toString();  // invalid syntax

When you include the second ., you have a decimal followed by the member operator.

(10.).toString();

@pedants and downvoters

The . character presents an ambiguity. It can be understood to be the member operator, or a decimal, depending on its placement. If there was no ambiguity, there would be no question to ask.

The specification's interpretation of the . character in that particular position is that it will be a decimal. This is defined by the numeric literal syntax of ECMAScript.

Just because the specification resolves the ambiguity for the JS interpreter, doesn't mean that the ambiguity of the . character doesn't exist at all.

15
  • why the interpreter decides so ? Is there any spec about that? I makes still no sense to me, the interpreter should treat it like a property because its much more likely.... Oct 30, 2012 at 23:58
  • stackoverflow.com/a/6853910/1026459
    – Travis J
    Oct 31, 2012 at 0:00
  • 8
    The . is not ambiguous. See my answer.
    – Alnitak
    Oct 31, 2012 at 0:02
  • @Alnitak: It presents an ambiguity because what would otherwise appear to be a valid placement of the same character could have two different behaviors. I'm not saying the specification is ambiguous. I'm saying the syntax is. The ambiguity is resolved in a specified manner. Oct 31, 2012 at 0:06
  • 3
    Lexers don't deal in ambiguity. Parsers do that. By the time these two expressions get to the parser the lexer has already made an unambiguous decision about which characters are grouped into which token.
    – Alnitak
    Oct 31, 2012 at 0:07
101

The lexer (aka "tokenizer") when reading a new token, and upon first finding a digit, will keep consuming characters (i.e. digits or one dot) until it sees a character that is not part of a legal number.

<152.> is a legal token (the trailing 0 isn't required) but <152..> isn't, so your first example reduces to this series of tokens:

<152.> <.> <toString> <(> <2> <)>

which is the legal (and expected) sequence, whereas the second looks like

<152.> <toString> <(> <2> <)>

which is illegal - there's no period token separating the Number from the toString call.

3
  • 7
    This is the clearest and most accurate answer, it should be accepted over the user1689607 one.
    – cweekly
    Nov 2, 2012 at 13:54
  • 3
    As a sidenote jsparse (jsparse.meteor.com) has a realtime js lexer/parser, where you can see that the js engine parses "10." as a number.
    – user1410117
    Nov 2, 2012 at 14:13
  • @SzabolcsKurdi: That thing is gorgeous! Jul 22, 2014 at 15:30
0

10. is a float number an you can use toString on float

eg.

parseFloat("10").toString() // "10"
6
  • 1
    Well 1 is a int and you can call int on a float too, the real problem is parser ambiguity, as user1689607 mentioned Oct 30, 2012 at 23:55
  • 3
    @JuanMendes: there is no such thing as int in javascript
    – georg
    Oct 30, 2012 at 23:56
  • 1
    @thg435 There's not a type called int, but internally ints and floats are represented differently. My point is that the parser reads is as an integer, and 10.toString() is ambiguous, it's impossible to tell if the . is for property access or the start of a float Oct 30, 2012 at 23:57
  • @JuanMendes the lexer and parser don't treat ints and floats separately. There's simply no such thing as an "integer literal" in JS.
    – Alnitak
    Oct 31, 2012 at 0:21
  • @JuanMendes (unless it's in hex, that is)
    – Alnitak
    Nov 1, 2012 at 9:02

Not the answer you're looking for? Browse other questions tagged or ask your own question.