Why does error 00.0 cause a syntax error?

This is strange. This is what happens on the JavaScript console in Chrome (version 42.0.2311.135, 64-bit).

> 0 < 0 > 00 < 0 > 0.0 < 0 > 00.0 X Uncaught > SyntaxError: Unexpected number 

Firefox 37.0.2 does the same, although its error message is:

 SyntaxError: missing ; before statement 

There are probably some technical explanations regarding how JavaScript parses numbers, and perhaps this can only happen when manipulating the console prompt, but it still seems wrong.

Why is he doing this?

+73
javascript numbers syntax-error
May 21 '15 at 8:54
source share
2 answers

Expressions 0.0 and 00.0 analyzed differently.

  • 0.0 parsed as a numeric literal 1
  • 00.0 parsed as:
    • 00 - octal numeric literal 2
    • . - property accessor
    • 0 - identifier name

Your code throws a syntax error because 0 not a valid JavaScript identifier. The following example works because toString is a valid identifier:

 00.toString 

1 Section 7.8.3 - Lead 0 may be followed by a decimal separator or ExponentPart
2 Section B.1.1 - Host 0 may be followed by OctalDigits

+77
May 21 '15 at 11:06
source share
โ€” -

00 is evaluated as an octal number, and .0 is evaluated as access to this number property. But since integers cannot be used as property attributes, an error occurs.

You get the same error for any other object:

 'string'.0 // Syntax error: unexpected number ({}).0 // Syntax error: unexpected number 

You can find the relevant property accessory information on MDN .

+22
May 21 '15 at 9:04
source share



All Articles