Just a quick question about the scoping of JavaScript variables.
Why does the alert()
function print the value of i
instead of returning undefined
?
$(document).ready(function () {
for(var i = 0; i < 10; i += 1){
}
alert(What is 'i'? + i);
});
I'm fairly new to JS, and in nearly all other languages I've dabbled, a declaration in the scope of the for loop would contain the value to that said loop, but not in this case, why?
i.e. What is 'i'? 10'
is printed.