3
     if(eggsAmount < eggsMin || milkAmount < milkMin || flourAmount || FlourMin)

Does it mean whichever of these are true?

gemma cass
  • 61
  • 4
  • That's generally what OR means, yes. If I said "get me eggs, or milk, or cheese", would getting any of these things fulfill my request? –  Jan 16 '20 at 20:30
  • It means if *"any"* of them are true. I'm not sure what you mean by "whichever" in this context. You can use `||` as a "falsy coalesce", but wrapped in an `if ( ... )`, you'll only know whether *at least one* was true or not. – Tyler Roper Jan 16 '20 at 20:30
  • 2
    Semantically, it means "if any of these are truthy, then the result is truthy". In terms of implementation, though, it will go through the list of terms from left to right, and as soon as it finds one that is truthy, it will return that term's value as-is. – IceMetalPunk Jan 16 '20 at 20:30
  • Also, you may want to specify what you believe this condition is. Some newer developers often think that `milkAmount < milkMin || flourAmount || FlourMin` means *"Milk Amount is less than milkMin, or it's less than flourAmount, or it's less than flourMin"*. This is **not the case**. You have to explicitly do `milkAmount < milkMin || milkAmount < flourAmount || milkAmount < FlourMin` if that's the case you're after. Though, not clear if you are. – Tyler Roper Jan 16 '20 at 20:33
  • @IceMetalPunk Close - it returns the result of the last expression evaluated. `1 + 1 || 1 + 3` returns `2` whereas `1 + 1 && 1 + 3` returns `4`. If the expression is a value itself, say `a = 123 ... a || b`, you get that value as-is since it's the result of the condition's expression. – Jared Farrish Jan 16 '20 at 20:33
  • @JaredFarrish The question was asking about `or`, not `and` :) In the case of `or`, returning the result of the last expression evaluated is equivalent to returning the first truthy value :) – IceMetalPunk Jan 16 '20 at 20:37
  • @IceMetalPunk The difference is when they're all falsey. It returns the last expression's value. – Barmar Jan 16 '20 at 20:38
  • @IceMetalPunk It doesn't return the value as-is unless it is a value from an expression, e.g. the condition evaluates as a value itself. Full stop. The inclusion of the second condition was to illustrate this further. Your choice. – Jared Farrish Jan 16 '20 at 20:39
  • @Barmar Ah! You're correct, I overlooked that edge case. – IceMetalPunk Jan 16 '20 at 20:39
  • I went in some sort of panic and deleted my answer, basically stating that the expression is globally evaluated as true is any of the conditions is true. But now I'm not sure why this statement was wrong even after consulting https://www.w3schools.com/js/js_comparisons.asp . Could anybody provide a source in which I can understand? – Roberto Caboni Jan 16 '20 at 20:50
  • @Cubo78 Open the console and enter `1 + 2 || 3 + 4 || 5 + 6`. You get `3` not `true`. This is a really interesting feature, functionally. – Jared Farrish Jan 16 '20 at 20:52
  • And compare to PHP: https://3v4l.org/l9VBj – Jared Farrish Jan 16 '20 at 20:55
  • Oh, ok. But within an if it makes the condition true, does not? I always used it for logic evaluation in my (amateur-ish) applications and not for strange C-like assignements. – Roberto Caboni Jan 16 '20 at 20:59
  • @Cubo78 `if` only evaluates if the result is truthy. – Jared Farrish Jan 16 '20 at 21:02
  • @JaredFarrish that was the OP was asking about. Anyway thanks to you I learned something new. Maybe with some edits I can salvage my answer. Ps: btw, this question (though is really basic) is not a 100% dupe of the linked question, because in that case the OP had wrongly used || instead of &&. – Roberto Caboni Jan 16 '20 at 21:09

2 Answers2

1

Yes, in an if statement with only OR || operators, it will simply go through each condition and as soon as it finds one that is true, the if statement resolves as true, the rest are not checked, and the code in the if block.

Stephen M Irving
  • 1,324
  • 9
  • 20
1

An if statement containing multiple OR conditions (||) has in Javascript the same behaviour descripted by Boolean Algebra: the whole evaluation is True if any of the conditions is true.

At this w3schools link you can find JS logic operators description.

Anyway the evaluation of the expression itself is not necessarily a boolean value. The expression

Res = expr1 || expr2 || ... || exprN

is evaluated as the first condition that can be evaluated as true (for example if expr1 is 4+7, Res=11.

If none of the condition can be evaluated as true the assigned value is the value contained in the last condition: Res = exprN.

(thanks to Jared Farrish, Barmar and Teemu)

Roberto Caboni
  • 7,252
  • 10
  • 25
  • 39