Do you know what will be the output of this code?
JavaScript
for (var i = 0; i < 3; i++) {
setTimeout(() => console.log(i));
}
Show answer and explanation
The answer is 3 3 3.
var creates a single i for the whole loop. The callbacks run after the loop has finished, when i is already 3, and all three closures read that same variable.
Changing var to let prints 0 1 2.