JavaScript问号&双管 [英] Javascript Question Mark & Double Pipes
问题描述
我是javascript新手,到目前为止,据我了解:
I am new to javascript and so far it is my understanding that:
?
& :
用于如果为true,请执行此操作,如果为false,请执行此操作"
?
& :
is used for "if true, do this, if false do this"
但是,我在||
上遇到了更多麻烦.从我的浏览看来,如果第一个是正确的,则执行此操作,否则执行此操作"
However, I am having a little more trouble with ||
. From my browsing it seems something like "if the first one is true do that, otherwise do this"
我正在尝试找出以下代码-在这种情况下有关它们的含义的任何建议吗?:
I am trying to figure out the following code - any suggestions on what they mean together in this context?:
function isSubset(series, description){
var subset = true;
var exactMatch = true;
demoCodes = ['age', 'edu', 'race', 'sex'];
for (var i = 0; i < demoCodes.length; i++){
var demoCode = demoCodes[i];
subset = (subset) ? (description[demoCode] == 0 || description[demoCode] == series[demoCode]) : false;
exactMatch = (exactMatch) ? description[demoCode] == series[demoCode] : false;
}
return {subset: subset, exactMatch: exactMatch};
}
谢谢!干杯
推荐答案
||
表示或".首先评估||
的左侧.如果解析为true,则表达式解析为true.另一方面,如果||
运算符的左侧解析为false,则将对右侧进行求值并返回.
||
means "or". The left side of the ||
is evaluated first. If it resolves to true, then the expression resolves to true. If, on the other hand, the left side of the ||
operator resolves to false, then the right side will be evaluated and returned.
示例1:
1 == 1 || 1 == 0
由于||的左侧,因此将评估为true.运算符为真.
Will evaluate to true, since the left side of the || operator is true.
示例2:
1 == 2 || 1 == 1
左侧解析为false,因此对右侧进行求值并返回.在这种情况下,1 == 1,因此整个表达式(1 == 2 || 1 == 1
)解析为true.
The left side resolves to false, so the right side is evaluated and returned. In this case, 1==1 so the whole expression (1 == 2 || 1 == 1
) resolves to true.
示例3:
1 == 2 || 1 == 3
左侧解析为false,因此对右侧进行求值并返回.在这种情况下,1不等于3,因此整个表达式(1 == 2 || 1 == 3
)解析为false.
The left side resolves to false, so the right side is evaluated and returned. In this case, 1 does not equal 3, so the whole expression (1 == 2 || 1 == 3
) resolves to false.
为了简单起见,如果任一表达式由||保持在一起"运算符为true,则表达式将返回true.否则,它将返回false.
To put it more simply, if either of the expressions "held together" by the || operator are true, then the expression will return true. Otherwise, it will return false.
这篇关于JavaScript问号&双管的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!