I recently had to fix a bug that manifested sometimes in Internet Explorer. The bug was that, sometimes, the parser choked on code like
<script type="text/javascript"> <!-- // comments --> /*...*/ </script> Which we fixed by correcting the comment. My question is: is "<!--" valid javascript code, or not? I tried testing it with firebug, and it only says" Undefined". JSFiddle didn't faze. IE only choked on it some of the times (reloading the page in question would show up the result of the script involved).
While knowing that, for historical reasons, an HTML comment inside js could be valid depending on its exact position and the phase of the moon is indeed useful, I was more interested in answers like "var <!-- foo is valid js code, but <!-- on its own is not. Here's why:..."
I did some analysis in firebug:
var x = 2; var y = 3; var z = 0; console.log(x); console.log(y); y<!--x; console.log(x); console.log(y); z = y<! --x; console.log(x); console.log(y); console.log(z); resulting in the following:
2 3 2 3 1 3 false The difference between the first and second tries is interesting.
I then tried
z = (y <!--x); console.log(z); Which failed with
SyntaxError: missing ) in parenthetical
foo <!--baris valid. :DReferenceError: foo is not definedfoo, and probably notbareither.