1

56.toString not works throws exception . 56..toString works fine .

Can anyone explain what is the difference between these two statements ?

56.toString(); //throws exception
56..toString(); //Works Fine.

How it works?

kannanrbk
  • 6,964
  • 13
  • 53
  • 94

3 Answers3

3

Numeric literals are somewhat special, because the property access . can be conflated with a decimal point .

When the parser encounters 56. it expects a digit (or nothing) to follow this. When you write 56..toString() you are getting the toString() of 56.0

Asad Saeeduddin
  • 46,193
  • 6
  • 90
  • 139
2

that's not problem for toString() method, just the 56. the toString will think it's 56.0, but it have not the '0', so it will fail.

work fine or like this:

(56.).toString()

or

56..toString()
BuddhiP
  • 6,231
  • 5
  • 36
  • 56
0

The problem here is when you type

56.toString()

JavaScript thinks 56 is a variable name, and you are trying to access it's toString() method. But this doesn't work because 56 is not a variable, and it's not even a valid variable name, therefore results in a compile error. (Variable name must start with a letter)

Check here for JS variable naming rules

But when you add a dot behind the 56 (56.) it becomes a number literal, and language creates an instance of Number class for that, then tries to resolve toString() method on the Number instance. Since the Number class defines a method called toString() and then it works.

56..toString();

Is equivalent to

(new Number(56.)).toString();

Key to understand here is difference between Number literal (56.) and method access operator (variable.methodName() )

BuddhiP
  • 6,231
  • 5
  • 36
  • 56
  • 1
    That isn't strictly true. `56` is parsed as a numeric literal, not an invalid variable name. The parser simply takes exception to the fact that you suffixed non numeric characters to the decimal point. For proof of this, note the fact that the exception thrown when you use `\`invalidvariable.toString()` is different from `56.toString()` – Asad Saeeduddin Nov 26 '12 at 08:04
  • @Asad, Interesting point, you may be right there. But your proof is not valid though. if you say invalidvariable.toString() it'll say invalidvariable is undefined, which is right. but if you say 3invalid.toString() you get the same error as 56.toString() (Uncaught SyntaxError: Unexpected token ILLEGAL for Chrome), I think that's more onto invalid variable naming. – BuddhiP Nov 26 '12 at 08:11
  • Note the illegal character I am inserting at the beginning of `\`invalidvariable` – Asad Saeeduddin Nov 26 '12 at 08:12
  • @Asad, Actually I did notice that. Still same result. In Chrome `x.toString(), 3x.toString(), 56.toString() all three return s the same "Uncaught SyntaxError: Unexpected token ILLEGAL" error. x.toString() on the other hand raise error "Uncaught ReferenceError: x is not defined" – BuddhiP Nov 26 '12 at 08:19
  • That's weird, I get `identifier starts immediately after numeric literal` in FF for the latter two, and `illegal character` for the former. – Asad Saeeduddin Nov 26 '12 at 08:21
  • I guess this behavior is verymuch dependant on the JavaScript parser used in different browsers – BuddhiP Nov 26 '12 at 08:31
  • However, I believe your explanation deserves more merit than mine. So I think 56 is treated as a numeric literal and it complaints about the non-digit character after the dot. – BuddhiP Nov 26 '12 at 08:43
  • You know, now I think about it I'm not entirely sure anymore. You could be right. Guess I'll have to go dig into the spec to find out for sure. – Asad Saeeduddin Nov 26 '12 at 08:47
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/20100/discussion-between-buddhip-and-asad) – BuddhiP Nov 26 '12 at 08:48