For example, this

<script> --> --> <!-- <!-- for (var i = 1; false < !--i;) console.log(i); <!-- <!-- console.log("hello"); <!-- <!-- </script> 

Browser fine. And this

 <script> for (var i = 1; false <!--i;) console.log(i); </script> 

mistake.

Similarly obtained for NodeJS. For example, I run NodeJS in interactive mode, and it turns out for the first code like this:

 > --> --> undefined > <!-- <!-- undefined > for (var i = 1; false < !--i;) console.log(i); 0 undefined > <!-- <!-- undefined > console.log("hello"); hello undefined > <!-- <!-- undefined > 

And for the second:

 > for (var i = 1; false <!--i;) console.log(i); ... 

Usually for interpreters in interactive mode, this means that the interpreter is waiting for input from a new line. Why?

But it works:

 > for (var i = 1; false < !--i;) console.log(i); 0 undefined > 
  • html! == javascript, why are you trying to insert a html comment in a javascript comment? More interesting why are you trying to do this, and not why it does not work) - Vasily Barbashev
  • I tried to do the same in NodeJS. The result is almost the same (the name of the error is different). And the first sample code NodeJS ate normally in console mode at least. - user239133
  • one
    here, there is some kind of html comment and the fact that the parser cannot trite the string, because in the first case there is a "less" sign, there is a negation and there is a decrement .. and in the second, the parser doesn’t bother the <!-- sign and throws an error ........ where are any html comments - unknown - Aleksey Shimansky
  • @Alexey, the first code fragment works both in the browser and in the node , and the second one is neither there nor there. - user239133
  • did you even read what i wrote? - Alexey Shimansky

1 answer 1

Everything turned out to be very simple. For backward compatibility, the sequence <!-- in any place is equivalent to the beginning of a single-line comment // . The sequence --> at the beginning of a line, before which there may be whitespace or a multiline comment /* ... */ , is also equivalent to the beginning of a single-line comment.