title: The use of undefined variables in if-statements
body: <p>This snippet results in a JavaScript runtime error: (<code>foo</code> is not defined)</p>
  
  <pre><code>if (foo) {
      // ...
  }
  </code></pre>
  
  <p>I have to define <code>foo</code> first, like so:</p>
  
  <pre><code>var foo = foo || null // or undefined, 0, etc.
  </code></pre>
  
  <p>... And only then can I do:</p>
  
  <pre><code>if (foo) {
      // ...
  }
  </code></pre>
  
  <p>Why is that?
  <br />
  <br />
  <br />
  <strong>Update:</strong></p>
  
  <p>This was somewhat of a brainfart on my side of things: 'fcourse you can't access a variable which is not allocated.</p>
  
  <p>Fun stuff that you can do a typeof() on an undefined variable thou. I'm gonna accept <a href="https://stackoverflow.com/users/66841/miccet">miccet</a>'s answer since I think it's the most elegant solution.</p>