Reputation: 309
See the code below:
2.toString(); // error
2..toString(); // "2"
2...toString(); // error
I want to know why 2..toString()
can run without errors and what happens when it runs?
Can somebody explain it?
Upvotes: 6
Views: 146
Reputation: 717
http://shamansir.github.io/JavaScript-Garden/en/#object
A common misconception is that number literals cannot be used as objects. That is because a flaw in JavaScript's parser tries to parse the dot notation on a number as a floating point literal.
2.toString(); // raises SyntaxError
There are a couple of workarounds that can be used to make number literals act as objects too.
2..toString(); // the second point is correctly recognized 2 .toString(); // note the space left to the dot (2).toString(); // 2 is evaluated first
Upvotes: 8