title: How to check if a variable has been declared even if it hasn't been defined?
body: <p>This question:</p>
  
  <p><a href="https://stackoverflow.com/questions/5113374/javascript-check-if-variable-exists-is-defined-initialized">JavaScript check if variable exists (is defined/initialized)</a></p>
  
  <p>does not regard the case of variables which have been declared but not defined (as opposed to undeclared and undefined). Specifically, if I want to check whether <code>x</code> has been declared, this:</p>
  
  <p><code>typeof x == 'undefined'</code></p>
  
  <p>will not do, because...</p>
  
  <pre><code>var x; //&lt;- declared but undefined
  // idontexist  not declared
  
  typeof idontexist === typeof x; // evaluates to true
  </code></pre>
  
  <p>This has implications: if variable has been declared it wont become a global variable, whereas if a variable has not declared, it would become a global variable, possibly leading to memory leak. </p>
  
  <p>So how do I check in javascript if undefined variable has been declared?</p>
  
  <p>Regarding the implications of this, consider this code:</p>
  
  <pre><code>function test() {
  
    var x
    console.log(typeof x == typeof y); // true
    if (typeof x == 'undefined') {
       x = 3;
    } 
    if (typeof y == 'undefined') {
       y = 5; 
    }
  }
  test();
  y; // 5
  x; // Reference Error
  </code></pre>