title: scope of the variable in JavaScript
body: <pre><code>function foo(a) {
    function a() {
      return 8;
    }
    return a();
    y = 9;
  }
  console.log(foo() + " " + y);
  </code></pre>
  
  <p>This gives an undefined error for variable <code>y</code>.</p>
  
  <pre><code>function foo(a) {
    y = 9;
  
    function a() {
      return 8;
    }
    return a();
  }
  console.log(foo() + " " + y);
  </code></pre>
  
  <p>This prints <code>8 9</code> in the browser console.</p>
  
  <p>If we declare a variable without the <code>var</code> keyword than it becomes global variable.</p>
  
  <p>Why first function does not follow the idea?</p>