-1

Why a = 0 || false returns false but not 0 in JavaScript?

Output from the debug console:

> a = 0 || false
false
> a
false

> a = 0
0
> a = a || false
false
> a
false
Systems Rebooter
  • 1,281
  • 2
  • 14
  • 30
  • 5
    My guess is that since `0` is "falsey" `a || false` resolves to `false` (the latter clause of the `||`), which means you're setting `a` to `false`. – David Sep 18 '18 at 11:36
  • Yep. You are right David! Just thought about the same once posted the question. Thanks! – Systems Rebooter Sep 18 '18 at 11:37
  • 1
    Why does your "output from debug console" not involve the expression you ask about in the title of your question? – Bergi Sep 18 '18 at 11:41
  • possible duplicate of https://stackoverflow.com/questions/5417969/why-dont-logical-operators-and-always-return-a-boolean-result – Bergi Sep 18 '18 at 11:42
  • Why do you expect it to return `0`? Did you read as `(a = 0) || false` (which still evaluates to `false`, but puts `0` in `a`)? – Bergi Sep 18 '18 at 11:45

4 Answers4

2
a = 0 || false

Let's decompose it, or let's follow javascript's logic.

  1. assign some expression to a
  2. the expression is being evaluated
  3. || tells us to pick first if it is truthy, otherwise pick second.
  4. 0 is NOT truthy, then pick the second option, which is obviously false
Nurbol Alpysbayev
  • 19,522
  • 3
  • 54
  • 89
1

a=0 is an assigment.

However, when you do a=0 || false, your expression converts to 0 || false, the result of which is false.

Aseem Upadhyay
  • 4,279
  • 3
  • 16
  • 36
  • `a = 0||false` is still an assignment! – Bergi Sep 18 '18 at 11:38
  • yes, assignment of the value of the condition to the variable – Aseem Upadhyay Sep 18 '18 at 11:39
  • What do you mean by "a condition"? The only expressions that I would call a condition are the first operand of the `? :` operator or what goes into an `if` head – Bergi Sep 18 '18 at 11:40
  • if you did a `return a = 0|| false` , you'll be returned a `false` value. how is this an assignment? – Aseem Upadhyay Sep 18 '18 at 11:41
  • It's still a return statement that contains an assignment expression – Bergi Sep 18 '18 at 11:43
  • 1
    @AseemUpadhyay: I thought you were correct but your comments clarify that you're also missing a piece of the answer. In all cases, there is an assignment (`a=...`). Whether or not the code line _ends_ with an assignment, that's something else. But in both of your code lines (in the edit), an assignment is happening. I suspect you're reading `=` as if it were a `==`. – Flater Sep 18 '18 at 11:44
  • true, my mistake. i got confused a little bit. misalignment of words. thanks a lot – Aseem Upadhyay Sep 18 '18 at 11:46
0

a = a is no logical comparison but just an assign, so all you do is 0 = 0 || false and since 0 is considered false

Why not 0 then ? because a condition always returns a boolean so 0 -> false

if you wanted to check if a equals a you'd have to write a == a


a = 0;

console.log(a, a = a || false, a == a || false );
flx
  • 1,560
  • 13
  • 22
  • "Why not 0 then ? because a condition always returns a boolean so 0 -> false" `a = a || 0` – tkausl Sep 18 '18 at 11:42
  • @tkausl it's not considered a condition anymore in this case. So it returns an integer because all values are integers – flx Sep 18 '18 at 11:44
  • `a = a || false` when `a = 1` doesn't return a boolean either. – tkausl Sep 18 '18 at 11:45
0

This is because the last evaluated value for the operator || is the operand false. This is in keeping with short circuit evaluation for logical operator. If your expression were:

a = 0 || 22

The output would be 22and not a boolean (true OR false). Explanation: same as above. The last evaluated expression is operand 22 and that is returned.

Let me know if that answered your question.

Ref: https://codeburst.io/javascript-what-is-short-circuit-evaluation-ff22b2f5608c

trk
  • 2,106
  • 14
  • 20