Let vs Var
Block-Scoped variables
function () {
var x = 1;
if (x) {
let y= x;
let x= "X";
y++;
}
console.log(x); // 1
console.log(y); // Error: y is not defined
}
// there is no hoist
function () {
console.log(foo); // ReferenceError
let foo = 2;
}
// very good to use in loops
var i=0;
for ( let i=i ; i < 10 ; i++ ) {
console.log(i);
}
// here, i is gone
// not very good to use with switch
switch (x) {
case 0:
let y= 'was 0';
break;
case 1:
let y= 'was 1'; // TypeError for redeclaration.
break;
}
// do not use this yet(let-block)
var x = 5;
var y = 0;
let (x = x * 2, y = 5) {
console.log( x + y ); // 15
}
console.log(x, y); // 5, 0