I
ieming.chen
I have a very puzzling question about how JavaScript variable scope
works.
var s = 1;
alert (s); // show s = 1
function show ()
{
alert(s); // show s = undefined, expecting s= 1
var s = 2;
alert(s); // show s = 2
};
Can anybody explain why it is happening this way?
works.
var s = 1;
alert (s); // show s = 1
function show ()
{
alert(s); // show s = undefined, expecting s= 1
var s = 2;
alert(s); // show s = 2
};
Can anybody explain why it is happening this way?