Does anyone know why the last alert in the code below complains that a is undefined. Shouldn’t a is 11 because it’s initialized in four() without using the var keyword? Thanks.
function three() {
var a = 12
function four() {
a = 11
function five() {
alert(a)
}
return five
}
return four
}
three()()()
alert(a)
No, because a has been declared with “var”, the assignment to a in four() refers to that declared variable, and the scope of a is limited to three().