for (var i = 0;i<10;i ){
function aa(){ console.log(i) }
}aa()//10
Why is 10 printed here?
console.log(i)
}What will be printed out is 9?
走同样的路,发现不同的人生
First case: After the loop ends, the value of i is 10, aa()outputs the value of i, so print 10. For example:
i
aa()
for (var j = 0; j < 10; j++) {} console.log(j); // print 10
Second case: i is printed in the loop, so in order to make the judgment condition successful, print 0 to 9.
First case: After the loop ends, the value of
i
is 10,aa()
outputs the value of i, so print 10. For example:Second case: i is printed in the loop, so in order to make the judgment condition successful, print 0 to 9.