和/或一起在 Javascript 中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17022778/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 06:49:18  来源:igfitidea点击:

And/or together in Javascript

javascript

提问by The Programmer

I'm wondering how to combine and (&&) with or (||) in Javascript.

我想知道如何在 Javascript 中将 and (&&) 与 or (||) 结合起来。

I want to check if either both a and b equals 1 or if both c and d equals 1.

我想检查 a 和 b 是否都等于 1 或者 c 和 d 是否都等于 1。

I've tried this:

我试过这个:

    if (a == 1 && b == 1 || c == 1 && d == 1)
    {
        //Do something
    }

But it doesn't work.

但它不起作用。

Does anyone know?

有人知道吗?

回答by David Foerster

&&precedes||. ==precedes both of them.

&&||. ==在他们两个之前。

From your minimal example I don't see, why it doesn't achieve your desired effect. What kind of value types do a–d have? JavaScript might have some non-obvious type coerciongoing on. Maybe try comparing with ===or convert to numbers explicitly.

从你的最小例子我看不出,为什么它没有达到你想要的效果。a-d 有哪些值类型?JavaScript 可能会进行一些不明显的类型强制。也许尝试===明确地与数字进行比较或转换为数字。

Side note:many lint tools for C-like languages recommend to throw in parentheses for readability when mixing logical operators.

旁注:许多用于类 C 语言的 lint 工具建议在混合逻辑运算符时放入括号以提高可读性。

回答by nnnnnn

Well now that I've finished telling everybody else (except David) why their answers are wrong, let me give them the same chance to hassle me.

既然我已经告诉其他人(除了大卫)为什么他们的答案是错误的,让我给他们同样的机会来找我麻烦。

Your existing code as shown should already do what you seemto be describing. But is it possible that when you say:

现有的代码,如图应该已经做什么你似乎被描述。但是当你说:

"I want to check if either both a and b equals 1 or if both c and d equals 1."

“我想检查 a 和 b 是否都等于 1,或者 c 和 d 是否都等于 1。”

...your use of the word "either" mean that you want to check if one and only oneof the following conditions is true:

...您使用“任一”一词的意思是您要检查以下条件中是否只有一个为真:

  • both a and b are 1, but c and d are not both 1
  • both c and d are 1, but a and b are not both 1
  • a 和 b 都是 1,但 c 和 d 不都是 1
  • c 和 d 都是 1,但 a 和 b 不都是 1

That is, you want one pair of variables to be 1, but you don't want all four variables to be 1 at the same time?

也就是说,您希望一对变量为 1,但您不希望所有四个变量同时为 1?

If so, that is an exclusive ORoperation, which in JS is the ^operator:

如果是这样,那就是一个异或操作,在 JS 中是^操作符

if ((a == 1 && b == 1) ^ (c == 1 && d == 1)) {

Note that unlike with a logical OR ||operator, you have to add parentheses around the AND &&parts of the expression because ^has higher precendence. (^is actually a bitwise operator, but it will work for you here since the operands you'd be using with it are all booleans.)

请注意,与逻辑 OR||运算符不同,您必须在表达式的 AND&&部分周围添加括号,因为它^具有更高的优先级。(^实际上是一个按位运算符,但它在这里对你有用,因为你要使用的操作数都是布尔值。)

回答by NINCOMPOOP

Operator Precedencecan be overridden by placing the expression between parenthesis.

可以通过将表达式放在括号之间来覆盖运算符优先级

 if ((+a == 1 && +b == 1) || (+c == 1 && +d == 1)) // Use brackets to group them
 {
   // your code
 }

This will prevent you from such cases like if(0&&0 || 1&&1).

这将防止您遇到诸如if(0&&0 || 1&&1).

回答by Anna.P

place some extra brackets to differentiate the and n or conditions

放置一些额外的括号来区分和 n 或条件

if ((a == 1 && b == 1) || (c == 1 && d == 1))