How does (A == B == C) comparison work in JavaScript?
Asked Answered
N

7

7

I was expecting the following comparison to give an error:

var A = B = 0;
if(A == B == 0)
    console.log(true);
else
    console.log(false);

but strangely it returns false.

Even more strangely,

console.log((A == B == 1)); 

returns true.

How does this "ternary" kind of comparison work?

Nessa answered 10/4, 2014 at 6:45 Comment(2)
A == B is true and true is not 0Gunthar
You can't compare like that, if (A==0 && B==0)Dogfish
T
8

First, we need to understand that a == comparison between a number and a boolean value will result in internal type conversion of Boolean value to a number (true becomes 1 and false becomes 0)

The expression you have shown is evaluated from left to right. So, first

A == B

is evaluated and the result is true and you are comparing true with 0. Since true becomes 1 during comparison, 1 == 0 evaluates to false. But when you say

console.log((A == B == 1));

A == B is true, which when compared with number, becomes 1 and you are comparing that with 1 again. That is why it prints true.

Trusting answered 10/4, 2014 at 6:50 Comment(0)
T
8

Assignment operators like = are right-associative: when there is a series of these operators that have the same precedence, they are processed right-to-left, so A = B = 0 is processed as A = (B = 0) (B = 0 returns 0, so both A and B end up as 0).

Equality operators like == are left-associative: same-precedence operators are processed left-to-right. A == B == 0 is processed as (A == B) == 0, and since A == B is true (1), it becomes 1 == 0, which is false (0).

Likewise, A == B == 1 is processed as (A == B) == 1, which becomes 1 == 1, which is true (1).

Source and more info: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence

Tomtom answered 10/4, 2014 at 6:54 Comment(0)
T
2

First, A == B returns true, which is then compared to 0, true == 0 which returns false, or true == 1 which returns true.

Thereabout answered 10/4, 2014 at 6:49 Comment(0)
I
2

It first checks your clause for A == B, which is true, than it starts checking true == 0, and 0 is false. So when you check A == B == 1, you check A==B, which is true, and true == 1. So then it returns true. If you really want to check all possibilities you should do something like this:

if((A==B) && (A==0))
   console.log(true);
else 
   console.log(false);
Illampu answered 10/4, 2014 at 6:49 Comment(0)
Y
1
if((A == B)&& (A== 0)&& (B==0))
    console.log(true);
else
    console.log(false);
Yuonneyup answered 10/4, 2014 at 6:47 Comment(0)
H
1

You can work from left to right. In this case you first check if A == B, then you check if this equal to 0. So since A == B, this is true. So now it becomes (true == 0), which is false. If A=1, B=2, then (A == B == 0) would return true! This is because A == B is false and (false == 0) is true!

Hazing answered 10/4, 2014 at 6:51 Comment(0)
M
0

I believe you should just need 2 comparisons here, but they do need to be independent. So just check:

(A==B && B=0) ? {if true this happens} : {if False this happens}

This should work the same as a triple comparison, unless you have 3rd condition in case B === 0 and A !== 0 being different from A ===0 and B !==0, eliminating the need for comparing A and 0.

Monecious answered 19/11, 2023 at 23:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.