I'm confused why the option 1 prints out '6', 5 times, whereas the option 2 prints out 0,1,2,3,4,5. Shouldn't the first one print out like the option 2?
Option 1:
for (var i = 0 ; i <= 5; i++) {
setTimeout(function() {
console.log(i);
});
}
Option 2:
for (var i = 0 ; i <= 5; i++) {
doThis(function() {
console.log(i);
});
}
function doThis(a) {
a();
}