
Hiep L. answered 04/28/19
Current Computer Science Professor with 30+ yrs of IT experience
Let has block scope, while var has function scope. For example, the code below is written inside a function:
for (var i =0; i < 5; i++) {
document.write(i);
}
document.write(i);
}
The output is 012345
If you use let instead of var in the above code for the variable i, the last write (the one OUTSIDE of the for loop) will give you an error as "i is not defined". Thus, var has the function scope so the variable i is "visible" after the for loop, whereas the let has visibility only inside the for loop but not outside of it.