Javascript Weird Dot Operator Syntax
In Chrome console, also test in edge and firefox 5.toFixed(2); get Uncaught SyntaxError: Invalid or unexpected token in chrome. SyntaxError: identifier starts immediately afte
Solution 1:
This is because of the JavaScript parser assuming the dot in for example 5.toFixed(2)
belongs the number literal. (As in 5.
, which is a valid number literal.) This is because JavaScript parses (at least number literals) greedily.
If you do (5).toFixed(2)
however, it is clear to the parser what you want (the dot clearly is not a part of the number literal).
Same with 5.1.toFixed(2)
. The second dot clearly cannot belong to the number literal, so the parser has a better time with it.
Post a Comment for "Javascript Weird Dot Operator Syntax"