> 2.toString() Uncaught SyntaxError: Invalid or unexpected token > 2..toString() "2"
2..toString() works is something I always found puzzling but never got to the bottom of it… until now, when I found myself with some free time and this question back in my mind.
2 it expects to be parsing a number, so
2 is valid. The next character,
., is also valid in a number (decimal number, but still, valid). Now the following character,
t, is not valid in a number, so an error is thrown:
Uncaught SyntaxError: Invalid or unexpected token.
In the second case,
2..toString(), it processes everything the same way but, when it runs into the second dot, it knows it cannot be a number since it found one dot before, the decimal separator. So the number it has so far (
2.) gets converted to a
2.0 which is
2) and them
toString is called on it, finally returning
"2". A simple way to allow a
toString() invocation on a number would be to wrap that number with parentheses, clearly encapsulating the number evaluation:
2..toString() is the same as having
> 2..toString() "2" > 2.0.toString() "2"