3

I was debugging on chrome console and found this error. Basically,

Math.ceil(0.1); // Works
Math.ceil(00.1); // Doesn't work

Any reason / thoughts for this?

Thanks.

Tpn Knvl
  • 43
  • 4

1 Answers1

2

Decimal literals can only start with a single 0. From the spec, a NumericLiteral is a DecimalLiteral, BinaryIntegerLiteral, OctalIntegerLiteral, or HexIntegerLiteral. Yours is a DecimalLiteral because it includes a .. That can have only one leading zero.

Here are the linked rules:

NumericLiteral::

     DecimalLiteral
     BinaryIntegerLiteral
     OctalIntegerLiteral
     HexIntegerLiteral

Yours is a DecimalLiteral because of the ., which is:

DecimalLiteral::

     DecimalIntegerLiteral . DecimalDigitsopt ExponentPartopt
     . DecimalDigitsopt ExponentPartopt
     DecimalIntegerLiteral ExponentPartopt

...where

DecimalIntegerLiteral::

     0
     NonZeroDigit DecimalDigitsopt

and

DecimalDigits::

     DecimalDigit
     DecimalDigits DecimalDigit

and

DecimalDigit:: one of

     0 1 2 3 4 5 6 7 8 9

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • 1
    I'm curious...any idea why JS throws this specific error in the OP's case? I'm just curious as to why JS thinks a closing parenthesis is missing. – JoshG Dec 30 '17 at 11:49
  • 1
    My understanding is that parser gets confused by the syntax and basically consumes the rest of the string (including the closing parenthesis). And that means it never goes out of 'looking for the end of arguments definition' state, which triggers the error. – raina77ow Dec 30 '17 at 11:52
  • 1
    @AndroidNoobie: The paren is missing after the `00`. `00` is interpreted as an integer literal, the `.` as method call syntax, but then it sees the `1` (which is not a legal method name). Since it was expecting a close paren eventually, and the code went off the rails when you tried to "call" `1` on it, it seems to backtrack and say "you probably missed a paren after the `00`". You can get a similar error (`SyntaxError: missing ; before statement`) doing just `00.1;` (or `foo.1;` for any old object), for roughly the same reasons. – ShadowRanger Dec 30 '17 at 11:56