title: Why does typeof only sometimes throw ReferenceError?
body: <p>In Chrome and Firefox,</p>
  
  <pre><code>typeof foo
  </code></pre>
  
  <p>evalulates to <code>'undefined'</code>.</p>
  
  <p>But</p>
  
  <pre><code>typeof (function() { return foo; })()
  </code></pre>
  
  <p>throws an error:</p>
  
  <pre><code>ReferenceError: foo is not defined
  </code></pre>
  
  <p><strong>This destroys the notions that I have of susbstitutability of expressions!</strong> Until now, I knew of no conditions for which <code>foo</code> and <code>(function() { return foo; })()</code> are not the same.</p>
  
  <p>Is this standard behavior? If so, it would be helpful to quote the relevant part of the ECMAScript standard.</p>
  
  <hr>
  
  <p>EDIT:</p>
  
  <p>Another example:</p>
  
  <pre><code>typeof (foo)
  typeof (foo + 0)
  </code></pre>
  
  <p>I would have expect <code>(foo)</code> and <code>(foo + 0)</code> to throw an error.</p>
  
  <p>But the first one has no error; the second one does.</p>