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

发布于 2024-08-24 06:32:38 字数 174 浏览 9 评论 0原文

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

if (A && B) { do something }

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

if (A OR B) { do something }

I understand that in JavaScript you can write:

if (A && B) { do something }

But how do I implement an OR such as:

if (A OR B) { do something }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

逆蝶 2024-08-31 06:32:39

使用逻辑“OR”运算符 ,即||

if (A || B)

请注意,如果在条件中使用字符串比较,则需要对每个条件进行比较:

if ( var1 == "A" || var1 == "B" )

如果只在第一个条件中进行比较,那么它将始终返回 true:

if ( var1 == "A" || "B" ) //don't do this; it is equivalent to if ("B"), which is always true

官方 ECMAScript 文档可以可以在此处找到

Use the logical "OR" operator, that is ||.

if (A || B)

Note that if you use string comparisons in the conditions, you need to perform a comparison for each condition:

if ( var1 == "A" || var1 == "B" )

If you only do it in the first one, then it will always return true:

if ( var1 == "A" || "B" ) //don't do this; it is equivalent to if ("B"), which is always true

The official ECMAScript documentation can be found here

旧竹 2024-08-31 06:32:39

值得注意的是,如果 AB 均为 true|| 也会返回 true >。

在 JavaScript 中,如果您要查找 AB但不是两者都,则需要执行类似于以下操作的操作:

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

Worth noting that || will also return true if BOTH A and B are true.

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

if( (A && !B) || (B && !A) ) { ... }
不甘平庸 2024-08-31 06:32:39
if (A || B) { do something }
if (A || B) { do something }
秋心╮凉 2024-08-31 06:32:39

|| 是或运算符。

if(A || B){ do something }

|| is the or operator.

if(A || B){ do something }
饮惑 2024-08-31 06:32:39

这是我的例子:

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

这表示如果答案是“是”或“是”,则会发生同样的事情

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

々眼睛长脚气 2024-08-31 06:32:39

也可以使用正则表达式

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

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

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

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

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

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

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.

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

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

祁梦 2024-08-31 06:32:39

您可能还想在条件 1 等于“某事”且条件 2 等于“另一件事”或“其他事物”时过滤 IF 语句。您可以通过将条件2放在另一组括号中来完成此操作,例如...

if (condition1 === 'x' && (condition2 === 'y' || condition2 === 'z')) {
    console.log('do whatever')
}

如果条件2在它自己的括号中NOT,则条件1 必须是 x 并且条件2 必须是 y 才能触发该函数...

...但如果条件2 是 z,它也会触发,无论如何条件1是什么。根据您的用例,这可能没问题,但需要注意一些事情。

You may also want to filter an IF statement when condition1 equals 'something' AND condition2 equals 'another thing' OR 'something else'. You can do this by placing condition2 in another set of brackets eg...

if (condition1 === 'x' && (condition2 === 'y' || condition2 === 'z')) {
    console.log('do whatever')
}

If condition2 is NOT in it's own brackets then condition1 has to be x AND condition2 has to be y for the function to trigger...

... but it will also trigger if condition2 is z, regardless of what condition1 is. Which may be okay depending on your use case, but something to be mindful of.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文