Code QuizBeginner

let creates a new binding per loop iteration

Using var in a loop with async callbacks captures one shared binding, while let creates a fresh binding each iteration.

Codejavascript
for (var i = 0; i < 3; i++) {
  setTimeout(function () {
    console.log(i);
  }, 100);
}
// Expected output: 0, 1, 2

The code is meant to log 0, 1, 2 but logs 3, 3, 3. What is the bug and how do you fix it?