题目描述:输出正确答案
console.log([2,1,0].reduce(Math.pow));
console.log([].reduce(Math.pow));
答案:
1
//报错
Uncaught TypeError: Reduce of empty array with no initial value
解析:
- arr.reduce(callback,[initialValue])
- reduce 接收两个参数,一个回调,一个初始值,回调函数接受四个参数:previousValue,currentValue,currentIndex,array
- 需要注意的是 如果是空数组,并且没有提供 initialValue,将会抛出错误
- 所以第二个会报错,第一个表达式等价于 Math.pow(2,1) = >2;Math.pow(2,0)=>1