admin管理员组文章数量:1429750
I am new to javascript and so far it is my understanding that:
?
& :
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};
}
Thanks! Cheers
I am new to javascript and so far it is my understanding that:
?
& :
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};
}
Thanks! Cheers
Share Improve this question asked Oct 29, 2014 at 17:11 As3adTintinAs3adTintin 2,47612 gold badges35 silver badges62 bronze badges 3- 1 MDN Documentation for Ternary Operator and Logical OR – epascarello Commented Oct 29, 2014 at 17:15
- ` if (subset) { if (description[demoCode] == 0) { subset = true; } else { subset = (description[demoCode] == series[demoCode]); } } else { subset = false; }` – epascarello Commented Oct 29, 2014 at 17:22
-
You might find this analysis of
||
vs&&
vs??
useful. – Inigo Commented Nov 28, 2021 at 12:06
2 Answers
Reset to default 3||
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.
Example 1:
1 == 1 || 1 == 0
Will evaluate to true, since the left side of the || operator is true.
Example 2:
1 == 2 || 1 == 1
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.
Example 3:
1 == 2 || 1 == 3
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.
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.
subset = (subset) ? (description[demoCode] == 0 || description[demoCode] == series[demoCode]) : false;
is equal to
if(subset){
subset = (description[demoCode] == 0 || description[demoCode] == series[demoCode);
}
else { subset = false; }
The ||
is an or operator here and evaluates to true
or false
本文标签: functionJavascript Question Mark amp Double PipesStack Overflow
版权声明:本文标题:function - Javascript Question Mark & Double Pipes - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745486039a2660387.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论