title: Chrome - Javascript engine parsing behavior
body: <p>HTML comments are allowed in Javascript, for older browsers which are not aware of <code>&lt;script&gt;</code> tags.</p>
  
  <p>Ref: <a href="https://stackoverflow.com/questions/808816/are-html-comments-inside-script-tags-a-best-practice">Are HTML comments inside script tags a best practice?</a></p>
  
  <p>So, the JS parser allows <code>&lt;!--</code> at the start of script, which will be ignored for parsing. And, <code>--&gt;</code> should be prefixed with <code>//</code> so that JS engine ignores them too.</p>
  
  <p>Ref W3C: <a href="https://www.w3.org/TR/REC-html40/interact/scripts.html#h-18.3.2" rel="nofollow noreferrer">https://www.w3.org/TR/REC-html40/interact/scripts.html#h-18.3.2</a></p>
  
  <p>While playing with this, I saw this weird behaviour.</p>
  
  <pre><code>&lt;script&gt;
      &lt;!--
      alert(1);
      --&gt;
  &lt;/script&gt;
  </code></pre>
  
  <p>Here, note that <code>--&gt;</code> isn't prefixed by <code>//</code>, which should throw an error by Chrome JS engine. But, Chrome JS engine consider them to be comments, and ignore parsing. Why? (Q1)</p>
  
  <p>And, another behaviour:</p>
  
  <pre><code>&lt;script&gt;
      alert(1);
      --&gt;''&gt;{][}
  &lt;/script&gt;
  </code></pre>
  
  <p>This snippet will execute alert. Here, note the place of <code>--&gt;</code>. This specific sequence of characters makes the parser <code>ignore</code> all other incorrect values following it. 
  What I mean is, in place of <code>--&gt;</code>, use any other character/sequence, it will throw error.</p>
  
  <p>So, what's happening here? How <code>--&gt;</code> makes this snippet valid? (Q2)</p>
  
  <p>Note: <code>--&gt;</code> sequence should be prefixed with a new line. That is,</p>
  
  <pre><code>&lt;script&gt;
      &lt;!--
      alert(1); --&gt;''&gt;{][}
  &lt;/script&gt;
  </code></pre>
  
  <p>will throw an error. Again, why? (Q3)</p>