I am reading the topics about the execution context and scope of javascript.Below is a simple code:
var scope="global";
function t(){
alert(scope); // alert :"undefined"
var scope="local" ;
alert(scope); // alert: "local"
}
t();
If I remove 'var scope="local" ; ' ,it becomes this:
var scope="global";
function t(){
alert(scope); // alert :"global"
}
t();
I don't understand why the value of scope is changed to "global" in the second case after I remove var scope="local" in the function t().
Could someone help to explain this, thank you!
alertshowsundefinedin the first example?scopeisn't initialized yet. You can take a look at the link in the @Tim's answer.