Main difference is scoping rules. Variables declared by
var keyword are scoped to the immediate function body (hence the function scope) while let variables are scoped to the immediate enclosing block denoted by { } (hence the block scope// demo: var
for(var i =0 ; i<5 ; i++){
console.log(i)
}//finally i =5
console.log(i) // i=5
// demo: let
for(let i = 0; i<5;i++){
console.log(i)
}
console.log(i)// i is undefined
function varTest() { var x = 1; if (true) { var x = 2; // same variable! console.log(x); // 2 } console.log(x); // 2 } function letTest() { let x = 1; if (true) { let x = 2; // different variable console.log(x); // 2 } console.log(x); // 1 }