ES7
2022/4/23 15:39 小于 1 分钟
ECMAScript 7(简称ES7)是于2016年6月正式发布的JavaScript语言的标准,正式名为ECMAScript 2016(ES2016)。
与第六个版本(ES6)相比变化并不多,主要是增加了两个新特性:Array.prototype.include和取幂运算符。
# Array.prototype.includes()
查找数组中是否有符合条件的元素
语法:
arr.includes(searchElement);
arr.includes(searchElement, fromIndex)
1
2
2
参数:
- searchElement:必须。需要查找的元素值。
- fromIndex:可选。从该索引处开始查找 searchElement。如果为负值,则按升序从 array.length + fromIndex 的索引开始搜索。默认为 0。
返回值:布尔值。如果找到指定值返回 true,否则返回 false。
示例:
const arr = [1, 2, 3, 4];
const arr1 = ['0', '1', '2', '3', '4'];
console.log(arr.includes(2)); // true
console.log(arr1.includes(0)); // false
1
2
3
4
5
2
3
4
5
- 如果fromIndex 大于等于数组长度 ,则返回 false 。该数组不会被搜索。
- 如果 fromIndex 为负值,计算出的索引将作为开始搜索searchElement的位置。如果计算出的索引小于 0,则整个数组都会被搜索。
# 指数操作符
var num = Math.pow(3, 3);
var num2 = 3 ** 3;
console.log(num, num2); // 27 27
1
2
3
2
3