(function() {
var index = 0;
var length = 5;
for (var index = 0; index < length; index++) {
setTimeout(function() {
console.log(index);
}, 100);
}
})();
This code outputs 55555
If I change the var to a let, it outputs 12345
I understand that let prevents the index variable from being hoisted to the top of the function scope but, I don't understand how this changes the answer.