JavaScript : Detecting result of a logical OR condition

标签 javascript loops if-statement conditional-statements logical-operators

是否有内置方法可以检测逻辑或条件中的哪一半为真?

// foo would return true or false    
if (thing1 === "3" || thing2 === foo()) {
     // do something if thing1 is 3
     // do something different if thing2 is true    
};

或者我应该只使用两个嵌套的 if block ?

最佳答案

使用两个 if block 。没有内置方法可以执行此操作。

此外,如果 foo() 返回 true 或 false,则不需要 thing2 === foo() 并且末尾的分号是无关的。

if (thing1 === "3") {
     // do something 
} else if (foo()) {
     // do something else
}

关于JavaScript : Detecting result of a logical OR condition,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34562956/

相关文章:

javascript - jquery验证密码强度大小写

Javascript - 创建对象并作为参数传递的正确方法

python - 计算字典键的总和

c++ - 声明一个 String 类型的数组,其中包含带有填充的数字

c++ - 有没有更好的方法可以编写这个程序?

javascript - 如何在 native 字符串原型(prototype)函数中使用 jQuery?

javascript - Nodejs Fstream 在管道传输多个文件时触发多个 'end' 回调

python - Reduce 给出错误,而 for 循环工作正常

javascript - 如何使用 If 语句在 Javascript 中对不同 ID 使用相同的函数

java - 使用反射加载类时避免 ClassNotFoundException