var zero = 0;
zero.toString(); // '0' --> fine
0.toString(); // syntax error!
0..toString(); // '0' --> fine
My conclusion: calling x.toString() doesn’t only depend on the value of x, but also on the way x is presented.
Are there other such examples in JavaScript where I might get an unexpected syntax error due to presentation?
Well, there are other cases where the context of where symbols appear affects how they behave, for example, statement Block’s vs Object Literals:
They look exactly the same, but blocks are evaluates in “statement context”, object literals are evaluated in expression context.
Also, Function Declarations vs. Function Statements, e.g.:
The example you post, relates to the way the grammar of Numeric literals are defined, the decimal part after the dot is actually optional, for example:
Is a valid Numeric literal, that’s why, accessing
0.toStringgives you aSyntaxError, the interpreter would expect the decimal part, instead thescharacter.See also:
1..somethingmean in JavaScript?