var arr = [];
for(var i=0; i<5; i++){
arr[i] = function(){
return i;
};
}
document.write(arr[1]());
the output is 5, as I expected
but when i added i++ between return i; and the end of the for loop, like the code below,
var arr = [];
for(var i=0; i<5; i++){
arr[i] = function(){
return i;
};
i++;
}
document.write(arr[1]());
screen shows the error, Uncaught TypeError: arr[1] is not a function
i expected that output should be 6, but i cannot understand why.
iwill be2. I suggest using some debugging tools to look at your variables or add someconsole.log()'s to your code to understand what's going on next time.arrto output the same value? That's what will happen.