Why does 00.0 cause a syntax error?

后端 未结 2 388
旧时难觅i
旧时难觅i 2021-02-02 05:10

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

> 0
< 0
         


        
2条回答
  •  时光说笑
    2021-02-02 05:43

    The expressions 0.0 and 00.0 are parsed differently.

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

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

    00.toString
    

    1 Section 7.8.3 – Leading 0 can be followed by decimal separator or ExponentPart
    2 Section B.1.1 – Leading 0 can be followed by OctalDigits

提交回复
热议问题