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