如何在 JavaScript IF 语句中使用 OR 条件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2363840/
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-08-22 23:57:11  来源:igfitidea点击:

How to use OR condition in a JavaScript IF statement?

javascriptboolean-expression

提问by sadmicrowave

I understand that in JavaScript you can write:

我知道在 JavaScript 中你可以这样写:

if (A && B) { do something }

But how do I implement an OR such as:

但是我如何实现一个 OR,例如:

if (A OR B) { do something }

回答by Luca Rocchi

Simply use the logical "OR" operator, that is ||.

只需使用逻辑“或”运算符,即||

if (A || B)

回答by user113716

Worth noting that ||will also return trueif BOTH Aand Bare true.

值得注意的是,如果 BOTH和are||也将返回。trueABtrue

In JavaScript, if you're looking for Aor B, but not both, you'll need to do something similar to:

在 JavaScript 中,如果您正在寻找Aor B但不是 both,您需要执行类似以下操作:

if( (A && !B) || (B && !A) ) { ... }

回答by Dolbz

if (A || B) { do something }

回答by rosscj2533

||is the or operator.

||是 or 运算符。

if(A || B){ do something }

回答by Dyljam1234

here is my example:

这是我的例子:

if(userAnswer==="Yes"||"yes"||"YeS"){
 console.log("Too Bad!");   
}

This says that if the answer is Yes yes or YeS than the same thing will happen

这表示如果答案是 Yes yes 或 Yes,那么同样的事情会发生

回答by jgshawkey

One can use regular expressions, too:

也可以使用正则表达式

var thingToTest = "B";
if (/A|B/.test(thingToTest)) alert("Do something!")

Here's an example of regular expressions in general:

以下是一般正则表达式的示例:

var myString = "This is my search subject"
if (/my/.test(myString)) alert("Do something here!")

This will look for "my" within the variable "myString". You can substitute a string directly in place of the "myString" variable.

这将在变量“myString”中查找“my”。您可以直接用字符串代替“myString”变量。

As an added bonus you can add the case insensitive "i" and the global "g" to the search as well.

作为额外的好处,您还可以将不区分大小写的“i”和全局“g”添加到搜索中。

var myString = "This is my search subject"
if (/my/ig.test(myString)) alert("Do something here");

回答by User that is not a user

If we're going to mention regular expressions, we might as well mention the switchstatement.

如果我们要提到正则表达式,我们不妨提到switchstatement

var expr = 'Papayas';
switch (expr) {
  case 'Oranges':
    console.log('Oranges are 
if(condition || condition){ 
   some stuff
}
.59 a pound.'); break; case 'Mangoes': case 'Papayas': // Mangoes or papayas console.log('Mangoes and papayas are .79 a pound.'); // expected output: "Mangoes and papayas are .79 a pound." break; default: console.log('Sorry, we are out of ' + expr + '.'); }

回答by KARTHIKEYAN.A

More then one condition statement is needed to use OR(||)operator in if conditions and notation is ||.

需要多个条件语句才能OR(||)在 if 条件和符号 is 中使用运算符||

if(condition1 || condition2 || condition3 || ..........)
{       
     enter code here
}

回答by Abhilash Reddy

You can use Like

你可以使用喜欢

##代码##