console.log(a);// undefined
var a =100;
console.log(b);// 報錯:b is not defined 找不到b這個變量
let b =10;
console.log(c);// 報錯:c is not defined 找不到c這個變量const c =10;
2.let和const聲明形成塊作用域
if(1){
var a =100;
let b =10;const c =1;}
console.log(a);// 100
console.log(b)// 報錯:b is not defined 找不到b這個變量
console.log(c)// 報錯:c is not defined 找不到c這個變量
3.var聲明的變量會掛載在window上,let和const不會
var a =100;
console.log(a,window.a);// 100 100
let b =10;
console.log(b,window.b);// 10 undefinedconst c =1;
console.log(c,window.c);// 1 undefined
4.同一作用域下var可以聲明同名變量,而let和const不可以
var a =100;
console.log(a);// 100
var a =10;
console.log(a);// 10
let b =100;
let b =10;// 控制檯報錯:Identifier 'b' has already been declared 標識符a已經被聲明瞭。const c=100;const c=10;// 控制檯報錯:Identifier 'c' has already been declared 標識符a已經被聲明瞭。