ES7新增特性
ES7是2016年发布的新特性。
1、新增数组方法includes()
includes()方法是用来判断数组是否存在某个元素,存在则返回true,不存在则返回false。
数组方法indexOf(),也是检测是否存在某个元素,并且返回元素的索引值。
两个方法的区别:
(1)includes()方法可以检测NaN数据,但是对于数组元素为空属性的情况,检测不正确,会检测为undefined类型,并且该数 组方法返回的是布尔值,不是元素的索引值。
(2)indexOf()无法检测NaN类型,而includes()方法可以检测。
let list = [1,'a',NaN,null,undefined];
console.log(list.indexOf(NaN));//-1
console.log(list.indexOf(1));//0
console.log(list.indexOf(null));//3
console.log(list.indexOf(undefined));//4
console.log(list.indexOf(2));//-1
console.log(list.includes(NaN));//true
console.log(list.includes(1));//true
console.log(list.includes(null));//true
console.log(list.includes(undefined));//true
console.log(list.includes(2));//false
//空属性数组元素
let listEmpty = new Array(3);//这样就可以创建一个数组,该数组有三个空属性值
console.log(listEmpty.includes(undefined));//true
console.log(list.indexOf(undefined));//-1
2、新增取幂运算符(**)
新增取幂运算符,简洁了运算代码的书写,例如:3**4相当于333*3
let a = 3;
console.log(a**2);//9,相当于a*a