Lowest price ever! Learn Generative AI for 48% less!
Get my discount+ 5
for(var i=0;i<5;i++){
setTimeout(function(){
console.log(i);
},1000);
}
console.log(i) //5
for(let i=0;i<5;i++){
setTimeout(function(){
console.log(i);
},1000);
}
console.log(i) //uncaught reference error. i is not defined
what this means is that let and const are block level scope. their declaration is visible only inside the function or block they are declared. block level scope in this situation means inside your for loop you just created
var on the other hand has global scope which means you can use it outside of a function if declared in that function