0

Why Output is undefined when we declare i as var in FOR loop but if declare it with let than it will run smoothly.

like in this case

const a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

for (var i = 0; i < 10; i++) {
  setTimeout(() => console.log(a[i]), 1000);
}

Output

undefined
undefined
undefined
undefined
undefined
undefined
undefined
undefined
undefined
undefined

0 Answers0