javascript - var A == var B == 1 looking for truthy. Not working. Is it possible? - Stack Overflow

Not sure this is doable, but I am looking for a truth return if, for example..var A = 100;var B = 100

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.

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.

Share Improve this question edited Feb 18, 2013 at 6:54 james emanon asked Feb 18, 2013 at 6:49 james emanonjames emanon 11.8k11 gold badges69 silver badges107 bronze badges 1
  • slightly shorter: (A==B) && (A==100) – Udo Klein Commented Feb 20, 2013 at 10:52
Add a ment  | 

5 Answers 5

Reset to default 13

Either it evaluates as

(A == B) == 100

or as

A == (B == 100)

In both cases you pare 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)));
A== 100 && B == 100

Is what you are looking for.

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

Because the after the second expression that is (B == 100) the value of A it gets pared to boolean so it would always be false

that is:

A == (B == 100)

Which bees

A == true

Which evaluated to false

So the correct version should be:

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

Live demo

i think it can be interpreted as

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

obviously,it's not true

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745309019a4621888.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信