es6变量

一 .let

1.let声明的变量 只在所处于的块级({})有效
如下

if(true){
let a = 10;
console.log(a)   // a=10 
}
console.log(a)  // a is not defined
 

使用let 关键字 声明的变量才具有 块级作用域 var关键字是不具备这个特点

if(true){
let num = 100;
var abc = 200;
}
console.log(num);  // is not defined
console.log(abc);  //  200

块级作用域 防止循环变量变成 全局变量

2.let声明的变量 不存在变量提升 (只能先声明 在使用)

console.log(a)  // a is not defined
let a =20

3.let声明的变量具有暂时性死区特性

var num = 10 
if (true) {
console.log(num) // num is not defined
  let  num = 20;
  
}

for 循环的时候 使用 let 关键字

二 const

作用:声明常量 常量就是值(内存地址) 不能变化的量
特点 1.也具有块级作用域
特点 2.声明常量时必须赋值

const PI ; //报错

特点 3.常量赋值后,值不能直接修改。

const PI = 3.174
PI = 100;   // 报错

const ary = [100,200 ];
ary[0] = 'a';
ary[1] = 'b';
console.log(ary); // ['a','b']

ary = ['a','b'];   //报错
变量.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。