Local variable impact global variable - Javascript variable scoping
<blockquote>
  <p><strong>Possible Duplicate:</strong><br>
  <a href="http://stackoverflow.com/questions/3725546/variable-hoisting">variable hoisting</a>  </p>
</blockquote>



<pre><code>    var a = "global";

    //version 1
    function changeGlobal(){
        alert(a); //why is 'a' undefined here?
        if(a){ 
        var a = "local";
        alert(a);
        }
    }
    //version 2
    function changeGlobal(){
        alert(a); //why is 'a' 'global' here? what diff does 'var a="local"' make?
        if(a){ 
        //var a = "local";
        alert(a);
        }
    }
    changeGlobal();
</code></pre>

<p>Question are inline. Help me understand variable scoping.</p>
