What is the difference between let and var?

What is the difference between let and var?

Let’s take an example
for(var i=0;i<5;i++){
console.log(i);
}
console.log(i);
output:
0 1 2 3 4 5
so var is a global variable and can be used outside of the loop as well whereas for let if we do the same which is
for(let i=0;i<5;i++){
console.log(i);
}
console.log(i);

output:
0 1 2 3 4 i was not defined which means it can’t be accessed globally.
let me know if it isn’t clear.