最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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

发布评论

评论列表(0)

  1. 暂无评论