difference between numbers using or without using variable

后端 未结 3 1670
盖世英雄少女心
盖世英雄少女心 2020-12-21 01:47

What\'s the difference between the following code?

var a = 1;
a.toString(); // outputs: \"1\"

But this throws an error:

1.t         


        
3条回答
  •  别那么骄傲
    2020-12-21 02:10

    Try to Change the Syntax,

    (1).toString()

    Numbers can have decimals, so the syntax for ending in a decimal is a bit ambiguous when you go to parse the code, use parenthesis to be valid. It's a bit clearer when you see that this is also valid:

    (1.).toString()

    However with just

    1.toString() it's trying to parse as a number with a decimal, and it fails.

提交回复
热议问题