james emanon
james emanon

Reputation: 11807

var A == var B == 1 looking for truthy. Not working. Is it possible?

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

Answers (5)

Udo Klein
Udo Klein

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

Vishal Suthar
Vishal Suthar

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)

Live demo

Upvotes: 1

movever
movever

Reputation: 29

i think it can be interpreted as

(A == B) && ((A == B) == 100) 

obviously,it's not true

Upvotes: 0

jagzviruz
jagzviruz

Reputation: 1513

It translates to true===100 which is obviously false. You can use a===b && b===100

Upvotes: 2

yunzen
yunzen

Reputation: 33439

A== 100 && B == 100

Is what you are looking for.

Upvotes: 2

Related Questions