Can somebody explain this?
1 == 1 //true, as expected
1 === 1 //true, as expected
1 == 1 == 1 //true, as expected
1 == 1 == 2 //false, as expected
1 === 1 === 2 //false, as expected
1 === 1 === 1 //false? <--
Also is there a name for boolean logic that pares more than two numbers in this way (I called it "three-variable parison" but I think that'd be wrong...)
Can somebody explain this?
1 == 1 //true, as expected
1 === 1 //true, as expected
1 == 1 == 1 //true, as expected
1 == 1 == 2 //false, as expected
1 === 1 === 2 //false, as expected
1 === 1 === 1 //false? <--
Also is there a name for boolean logic that pares more than two numbers in this way (I called it "three-variable parison" but I think that'd be wrong...)
2 Answers
Reset to default 7This expression:
1 === 1 === 1
Is evaluated as:
(1 === 1) === 1
After evaluating the expression inside parentheses:
true === 1
And that expression is logically false. The below expression returns true
as expected though:
1 === 1 === true
Equality is a left-to-right precedence operation.
So:
1 == 1 == 1
true == 1
true
And:
1 === 1 === 1
true === 1
false // because triple-equals checks type as well