Reputation: 11807
Not sure this is doable, but I am looking for a truth return if, for example.. var A = 100; var B = 100;
(A == B == 100)
I figured this would return true. Because A == B (they are both 100) and as such, they both equal 100.
But it is always false.
EDIT::: Thanks, Yeah - I appreciate the repsonses.. I was hoping there was some nifty shorhand than doing (A === 100 ) && ( B === 100) etc... But thank all very much.
Upvotes: 1
Views: 162
Reputation: 6882
Either it evaluates as
(A == B) == 100
or as
A == (B == 100)
In both cases you compare a boolean with 100. This is of course always false. You want
(A==100) && (B==100)
To see what is going on you might want to run the Example below as JSFiddle:
var A = 100;
var B = 100;
alert("B == 100: " + (B == 100));
alert("A == (B == 100):" + (A == (B == 100)));
alert("A == B:" + (A == B));
alert("(A == B) == 100:" + ((A == B) == 100));
alert("A == B == 100):" + (A == B == 100));
alert("(A == 100) && (B == 100):" + ((A == 100) && (B == 100)));
Upvotes: 13
Reputation: 17193
Because the after the second expression that is (B == 100)
the value of A
it gets compared to boolean
so it would always be false
that is:
A == (B == 100)
Which becomes
A == true
Which evaluated to false
So the correct version should be:
(A == 100) && (B == 100)
Upvotes: 1
Reputation: 29
i think it can be interpreted as
(A == B) && ((A == B) == 100)
obviously,it's not true
Upvotes: 0
Reputation: 1513
It translates to true===100
which is obviously false
. You can use a===b && b===100
Upvotes: 2