First up: I know this is not how one should do a comparison, this is merely a question of interest. Let's say you do this comparison:
var x = 0;
if(1 < x < 3) {
console.log("true");
} else {
console.log("false");
}
What is happening inside that if-statement so that the output is "true"? Is there some implicite logical comparison happening. And how do I find out?
The comparison takes place from left to right so 1 < x < 3
will evaluate as
1 < x
first which is false
, given that x
is 0
. Here the next comparison will be,
false < 3
which will be true
because there will be implicit type conversion of false
to numeric representation, which is 0
. So, the expression evaluates to 0 < 3
which is true
.
Hence, when you do true < 3
or false < 3
then this boolean value will be implicitly converted to 0
as false
and 1
as true
.