2

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

As3adTintin
  • 2,406
  • 12
  • 33
  • 59
  • 1
    MDN Documentation for [Ternary Operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator) and [Logical OR](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_Operators) – epascarello Oct 29 '14 at 17:15
  • ` if (subset) { if (description[demoCode] == 0) { subset = true; } else { subset = (description[demoCode] == series[demoCode]); } } else { subset = false; }` – epascarello Oct 29 '14 at 17:22
  • You might find this [analysis of `||` vs `&&` vs `??`](https://stackoverflow.com/a/70143185/8910547) useful. – Inigo Nov 28 '21 at 12:06

2 Answers2

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.

foxygen
  • 1,368
  • 1
  • 11
  • 22
  • 1
    ah, while your original answer made sense, the added edit with the bold clarification really helped me understand the logic in this function. thanks! – As3adTintin Oct 29 '14 at 17:28
1
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

Amit Joki
  • 58,320
  • 7
  • 77
  • 95