链式逻辑OR比较

时间:2017-10-21 15:13:44

标签: javascript logical-operators or-operator

为什么这不起作用:

if (x != (a || b || c)) {
  doStuff();
}

这是为了检查x是否不等于OR b OR c。

编辑:我如何检查x是否不等于OR b OR c?

编辑:好的,这是重复的。我现在该怎么做,即使在意识到我的错误之后也要拿减分? :P

1 个答案:

答案 0 :(得分:-2)

要像你想要的那样使用倍数值:



 
var x = 'x';
var a = 'a';
var b = 'b';
var c = 'c';

function doStuff() {
  console.log(1)
}

// exemple 1
if(x == a || x == b || x == c) {
   doStuff();
}

function isSameValue(element, index, array) {
  return element === x;
}

// exemple 2
if([a, b, c].some(isSameValue)) {
  doStuff();
}

// exemple 3
[a, b, c].includes(x);




相关问题