Talk:Core JavaScript 1.5 Reference:Statements:var
From MDC
It would be nice if there were some further explanation and examples of how var works inside loops (the declaration isn't local to the loop -- it's local to the external environment). For a quick example of this, I wasn't aware that:
function q()
{
while (true)
{
var y = 3;
break;
}
// y now in q's environment
alert(y); // alerts 3
while (true)
{
y = 7; // affects y in q's environment
break;
}
return y; // alerts 7
}
alert(q());
...is valid JavaScript (and, particular to the Mozilla JS engine, no strict warnings will be generated) prior to reading this page, and I think there are some edge cases my quick example doesn't cover (such as, for example, what should happen if var y = "foo"; is inserted as the first line in q). --Waldo 23:07, 17 April 2006 (PDT)