title: Why this global variable is undefined inside a function?
body: <p>Why is this global variable undefined inside a function if the same global variable is re-declared and defined inside that same function?   </p>
  
  <pre><code>var a = 1;
  function testscope(){
   console.log(a, 'inside func');
   //var a=2;
  };
  testscope();
  console.log(a, 'outside func');
  
  output:
  1 "inside func"
  1 "outside func" 
  </code></pre>
  
  <p>Consider same code where var a = 2; inside function block is uncommented</p>
  
  <pre><code>var a = 1;
  function testscope(){
   console.log(a, 'inside func');
   var a=2; 
  };
  testscope();
  console.log(a, 'outside func');
  
  Output
  undefined "inside func"
  1 "outside func"
  </code></pre>