我正在使用一个接受数组的函数,如果其所有值均为偶数,则返回true,否则返回false。
我的代码在下面,传递的数组是[-2,2,-2,2]
function checkAllEven(arr) {
return arr.every(x=> { x % 2 === 0 });
}
-------------
Output: False
但是当我除去花括号时,它可以正常工作并返回true。
function checkAllEven(arr) {
return arr.every(x=> x % 2 === 0 );
}
-------------
Output: True
只想知道为什么会这样。
如果有花括号,则应使用return
:
function checkAllEven(arr) {
return arr.every(x=> { return x % 2 === 0 });
}