indexOf() 不需要再比较数字

148 阅读1分钟
const arr = [1, 2, 3];
 
// 存在,等效于 > -1
if (~arr.indexOf(4)) {
    console.log("有")
}
 
// 不存在,等效于 === -1
if (!~arr.indexOf(4)) {
    console.log("没有")
}

这样就省去比较数字的环节了