I thought all the functions in setTimeout are executed in global scope. Then I saw this today:
for(let x = 0; x < items.length; x++){
setTimeout(function() {
console.log(x);
})
}
Even with a value for x in global scope/window scope; this code consoles from 0 to 9. What is I am missing here. Isn't this function supposed to run in global scope.
How come using let instead of var changes the former fact ?