title: Trouble understanding semicolon insertion
body: <p>According to <a href="http://inimino.org/~inimino/blog/javascript_semicolons" rel="nofollow">this</a>, JavaScript will insert a semicolon if:</p>
  
  <blockquote>
    <p>When the program contains a token that is <strong>not allowed by the formal grammar</strong>, then a semicolon is inserted if (a) there is a line break at that point, or (b) the unexpected token was a closing brace. (emphasis mine)</p>
  </blockquote>
  
  <p>The classic example is</p>
  
  <pre><code>return  //  &lt;--- semicolon inserted there
  {
     id: 12
  };
  </code></pre>
  
  <p>Which leads me to believe that a free standing <code>{</code> is not valid.  Yet the following (pointless) code alerts 2, without error</p>
  
  <pre><code>function foo() {
      var x = 1;
      {
          var y = 2; //yes, I know y has the same scope as x 
      }              //and that this is therefore pointless
      alert(y);
  }
  </code></pre>
  
  <p>Why is the opening brace treated as an invalid token in the first code, which leads JavaScript to insert a semicolon, but the opening brace is not treated as invalid in the second—as evidenced by the fact that there is no error.</p>
  
  <p>Clearly one of my assumptions is wrong, and I was hoping someone could help me understand which.</p>