1 – why when I run the below code I got undefind instead “a=1” ?
function f1(){a=1; f2();}
function f2(){return a;}
var a= 5;
a = f1();
alert(a);
like this example the resualt is “a=1”.
function f1(){a=1; f2();}
function f2(){alert(a);}
var a= 5;
f1();
With
you are assigning the result of calling
f1toa. Yet,f1does not return anything, it evaluates toundefined. You’d need to use areturnstatement:Btw, this is not a scope problem. You don’t have any variables that are local to your functions, everything accesses the same
a.