Menu
I’m not understanding why the code below returns the number 3, three times.
The piece of code
[CODE]for(var i=0; i<3; i++)
starts the value of i at 0. Then, it says as long as i is less than 3, the value increases by one. So, it seems to me the values of 1 and 2 would get passed to console.log(i) and then stop because it says i<3. What am I not undestanding?
Here’s the full program:
[CODE]
<!DOCTYPE html>
<html>
<body>
<p>
</p>
<script>
var a = [];
for(var i=0; i<3; i++) {
a[i] = function() { console.log(i) };
}
a[0]();
a[1]();
a[2]();
</script>
</body>
</html>