Sololearn: Learn to Code
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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
29th May 2020, 4:52 PM
Sebastian Pacurar
Sebastian Pacurar - avatar