var animel = new Array();
animel[0] = 'cat';
animel[1] = 'dog';
animel[2] = 'horse';
animel[3] = 'cow';
animel[4] = 'elephant';
animel[5] = 'tiger';
animel[6] = 'lion';
animel[7] = 'fish';
for (var i = 0; animel.length > i; i++) {
setTimeout( function () {
console.log(animel[i]);
}, 2000);
}
When I execute this code in console, it logs undefined
instead of the name of elements. What am I doing wrong in this?