javascript 如果节点 js 上的条件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/54719012/
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
if condition on node js
提问by Jomar Tinga
I'm Getting confused
我很困惑
I tried to have to have a two condition, in the first code I'm getting trueanswer seems the answer should be false.
我试图必须有两个条件,在我得到的第一个代码中,true答案似乎应该是false.
var a = 'route 3';
if(a === 'route 1' || 'route 2'){
console.log('true')
}else{
console.log('false')
}
on the second code, I have two conditions and the first condition it seems trueand the second must be falsebut i'm getting trueanswer.
base on my understanding if the first condition is falsethe second condition will not be evaluated. can you enlighten my newbie mind why i'm getting trueanswered.
在第二个代码中,我有两个条件,第一个条件似乎true是第二个,false但我得到了true答案。根据我的理解,如果第一个条件是false第二个条件,则不会进行评估。你能启发我的新手头脑为什么我得到true回答。
var a = 'route 1';
var b = 'route 3';
if(a === 'route 1' || 'route 2' && b === 'route 1' || 'route 2'){
console.log('true')
}else{
console.log('false')
}
thank you for the answer :)
谢谢你的回答:)
回答by George
You are forgetting to check if a is equal to 'route 2', when you simply do if('route 2'). After using ||, you still need to explicitly define the next condition, which is a === 'route 2'.
当您只是执行if('route 2'). 使用之后||,还需要明确定义下一个条件,即a === 'route 2'。
Instead use:
而是使用:
var a = 'route 3';
if(a === 'route 1' || a === 'route 2'){
console.log('true')
}else{
console.log('false')
}
Or even more concise:
或者更简洁:
var a = 'route 3';
console.log(a === 'route 1' || a === 'route 2');
回答by Word Rearranger
Changing your first code block to this will fix your problem:
将您的第一个代码块更改为此将解决您的问题:
var a = 'route 3';
if(a === 'route 1' || a === 'route 2'){
console.log('true')
}else{
console.log('false')
}
The || operator does not work the way you think. What you are saying is: is aequal to route 1? || is route 2... (instead of: is a equals to route 2). The same issue applies to your second example:
|| 运算符并不像您想象的那样工作。你说的是:a等于route 1?|| 是route 2...(而不是:等于路线 2)。同样的问题适用于您的第二个示例:
var a = 'route 1';
var b = 'route 3';
if(a === 'route 1' || a === 'route 2' && b === 'route 1' || b === 'route 2'){
console.log('true')
}else{
console.log('false')
}
More on operators: Javascript operators
有关运算符的更多信息:Javascript 运算符
回答by Train
In this statement
在这份声明中
if(a === 'route 1' || 'route 2' && b === 'route 1' || 'route 2')
if(a === 'route 1' || 'route 2' && b === 'route 1' || 'route 2')
the part that sais || 'route 2')will always evaluate to true.
sais|| 'route 2')将始终评估为真的部分。
it must be if(a === 'route 1' || (a === 'route 2' && b === 'route 1') || a === 'route 2')
一定是 if(a === 'route 1' || (a === 'route 2' && b === 'route 1') || a === 'route 2')
when you evaluate if('route 2')that would check for a value which always evaluates to true. You would have to compare it to something.
当你评估if('route 2')它会检查一个总是评估为真的值。你必须将它与某些东西进行比较。
回答by Jaime Argila
Problem:
问题:
'route2'returns truebecause there is no comparing ie. a === 'route2'the string will always return true
'route2'返回true因为没有比较即。a === 'route2'字符串将始终返回true
Solution:
解决方案:
var a = 'route 3';
if(a === 'route 1' || a === 'route 2'){
console.log('true')
}else{
console.log('false')
}

