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?
Best Answer
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
becomes1
andfalse
becomes0
)The expression you have shown is evaluated from left to right. So, first
is evaluated and the result is
true
and you are comparingtrue
with 0. Sincetrue
becomes1
during comparison,1 == 0
evaluates tofalse
. But when you sayA == B
istrue
, which when compared with number, becomes1
and you are comparing that with 1 again. That is why it printstrue
.