The Javascript parser is greedy (it matches the longest valid operator each time), so it gets the ++
operator from a++b
, making:
(a++) b
which is invalid. When you put in spaces a + + b
, the parser interprets it like this:
(a) + (+b)
which is valid and works out to be three.
See this Wikipedia article on Maximal munch for more details.