javaScript变量声明方式
javaScript声明变量,可以通过let、var、const来声明:
let:块级变量声明【es6新增的声明变量方式 】
作用域是块级作用域
1
2
3
4if(true){
let a = 1;
console.log(a);
}不存在变量提升
1
2console.log(a); //这里会报错:ReferenceError: a is not defined
let a = 1;不能重复定义
1
2let a = 1;
let a =2; //这里会报错:SyntaxError: Identifier 'a' has already been declared存在暂时性死区
1
2
3
4
5
6let a = 1;
if(1){
console.log(a); //这里会报错:ReferenceError: a is not defined,去掉这语句后,输出a结果为:2
let a = 2;
console.log(a);
}
到此,javaScript三种变量声明就介绍完了……
Original author: John Doe & wooyee.Landucheg
Original link: http://yoursite.com/2019/06/10/javaScript变量声明方式/
Copyright Notice: Please indicate the source of the reprint (must retain the author's signature and link)