Why does “a + + b” work, but “a++b” doesn’t?

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.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)