0

I've seen this type of example and I understand how to get around it to do a proper comparison against foo and bar (where they are 2 different numbers) but I don't understand why the value of bar is returned in this case instead of a true/false value. Would be good to understand the reason the last value is returned as this is an equality check and not an assignment.

let a = foobar === foo || bar 
j obe
  • 1,026
  • 2
  • 10
  • 17
  • 1
    Presumably `foobar === foo` is `false`, so the right side of the `||` is evaluated and returned, which would just be the value of `bar`. – David Jul 02 '20 at 11:55
  • 1
    Also relevant: [What does “var FOO = FOO || {}” (assign a variable or an empty object to that variable) mean in Javascript?](https://stackoverflow.com/q/6439579) | [JavaScript OR (||) variable assignment explanation](https://stackoverflow.com/q/2100758) | [What does the construct x = x || y mean?](https://stackoverflow.com/q/2802055) – VLAZ Jul 02 '20 at 11:56
  • its a 'short-circuit' evaluation ... 'a' take the 'bar' value is the things before the or is falsy ... – Andrelec1 Jul 02 '20 at 11:58
  • Thanks, yes foobar here can be 1, and foo =2, bar =3. I know with short circuit evaluation false || false equals false but here since the values are "2 || 3" and not equal to 1, then shouldn't false be returned also? – j obe Jul 02 '20 at 12:05
  • @VLAZ those links didn't help much, I'd already read one of them. they're talking about assignment with short circuit evaluation without an equality check happening. I'm wondering why the "===" isn't causing a true/false value to be returned? why isn't the comparison working. – j obe Jul 02 '20 at 12:10
  • 1
    @jobe read the duplicate and the other links. In short `||` returns the value of the first truthy expression *or* the the value of the last expression. It's not returning a boolean. With `2 || 3` the first expression `2` is truthy, so it will be returned. If you have, say `null || 3` then `null` is falsy but `3` is truthy, so you get `3`. With something like `null || 0`, both expressions are falsy buy you'd get the last one - `0`. – VLAZ Jul 02 '20 at 12:11
  • 1
    `foobar === foo` gets evaluated first and its result is used as the left operand for `||`. So after that the expression effectively becomes `false || 3` (`===` does indeed return `true` or `false`). If you want `foo || bar` to be evaluated as its own expression you need to wrap it in parenthesis, i.e: `foobar === (foo || bar)` – Lennholm Jul 02 '20 at 12:12
  • If `===` doesn't return `true` then you'd continue and return `bar`. That's it. If you always get `bar`, then the comparison is always `false` – VLAZ Jul 02 '20 at 12:12
  • 1
    @jobe: *"I'm wondering why the "===" isn't causing a true/false value to be returned? why isn't the comparison working."* - It is doing exactly that. And that operation is returning `false`. Which means the next operation being performed is `false || bar`. The result of that operation is the value of `bar`, as described in the linked questions. – David Jul 02 '20 at 12:30
  • Great thanks, so the true/false comparison is happening, it's just evaluated as the result on the left hand side, makes sense now! :) – j obe Jul 02 '20 at 12:41

0 Answers0