es6中let和const区别

1.let变量

  • let 声明的变量为局部变量,优点可以保证在某个域中变量不被其他变量污染
  • 不存在变量提升
  • 在同一个域下,不允许重复声明
 {
           let a = 'aaa'
           var a = 'nnn'    //禁止重复声明
        }

代码块内,使用let命令声明变量之前,该变量都是不可用的。这在语法上,称为“暂时性死区”

var tmp = 123;
	if (true) {
  		tmp = 'abc'; // ReferenceError
  		let tmp;
}

2.const
const 声明的变量为只读变量 (只能获取;不能改变)

{
	const a= "aaa",
	a=123;
	console.log(a)   //报错
}

猜你喜欢

转载自blog.csdn.net/weixin_44707104/article/details/88599775