In a book trying to illustrate global/local variable scope it used this example to convey the concept:
test();
if (isset(bar)){
alert('bar must be global!');
}else{
alert('bar must be local!');
}
function test(){
var foo = 3;
bar = 5;
}
function isset(varname){
return typeof varname != 'undefined';
}
Right now the if returns the alert 'bar must be global.', which makes sense because the variable passed to the isset()
, bar, in this case is global. But why doesn't the if statement return the alert('foo must be local'); when you pass lets say when you pass foo
in the isset function.