function myFunction()
{
var a =2;
if (a>0)
{
var a=4;
a++;
}
alert("A="+a);
}
The above code would display a message box saying "A=5"! This is because variables scope is either global, or at function-level. So, having {} blocks itself does not change the variable scope, and the "a" above is always the same variable. Since nested functions and anonymous functions are allowed, this can be one way to achieve more granularly defined variables.
1 comment:
Post a Comment