0%

ES6学习记录

JS中的var let const

  • var属于function scope 而 let和const属于block scope。

  • let和const属于ES6新特性。

  • const定义的变量类似Java中的final关键字,其值不可修改。

1
2
3
4
5
6
7
8
9
10
function(){
var a = 123;
if(a == 123){
let b = 456;
}
console.log(b); // Uncaught ReferenceError: b is not defined
const c = 789;
c = 0; // Uncaught TypeError: Assignment to constant variable.
}
console.log(a); // Uncaught ReferenceError: a is not defined