This thread inspired the question. Here are the code samples again. I'm looking for an answer that tells exactly what is going on.
Both x = 0; x+/*cmt*/+;
and var f/*cmt*/oo = 'foo';
produce syntax errors, which renders the answers in this question wrong.
You're interrupting a word instead of a sentence. ++ and foo are words. People assume you won't be interrupting those.
Much the same as you can't put whitespace in the middle of words even though whitespace is "safe".
From ECMAScript reference :
As many others have pointed out, the lexical parsing determines how things will become.
Let me point out some example:
So the resulting token list will be:
But if you do this:
The resulting token list will be:
Then same happens when comments inserted inside an operator.
So you can see that comments behave just like whitespace.
In fact, I recently just read an article on writing a simple interpreter with JavaScript. It helped me with this answer. http://www.codeproject.com/Articles/345888/How-to-write-a-simple-interpreter-in-JavaScript
Because comments are parsed at the lexical level, generally considered as whitespace.
When compiling, the first step is to lexically break it up into individual tokens. Comments are one type of token, and operators are another. You're splitting the ++ operator token so that it's interpretted as two separate items.