Why is 1===1===1 false?

久未见 提交于 2019-11-30 14:27:38

问题


In a browser console, entering 1===1 evaluates to true. Entering 1===1===1 evaluates to false.

I assume that this is because of the way the statement is evaluated:

1 === 1 === 1

becomes

(1 === 1) === 1

which evaluates to

true === 1

which is false.

Is this correct? If not, what's the real reason for this behaviour?


回答1:


Yes, you're exactly right. Here you have two equality checks, which have the same operator precedence. First one evaluates first, then its result applies to the next equality check.

1===1===1is the same as (1===1)===1 which is true===1 which is false, because here you check by values AND their types. 1==1==1 will result in true, because it checks equality by values only, so 1==1==1 equal to (1==1)==1 equal to true==1 equal to true.




回答2:


The === operator doesn't just test equality, but also type equality. Since an integer is not a boolean, true === 1 is false.

Compare:

true == 1; // true
true === 1; // false

Example.




回答3:


Correct behaviour. Since

1===1 // value is true

but

true===1 // it's false

There are two reasons for this:

  1. true is a boolean type where 1 is integer
  2. simply, 1 is not equal to true.

so

1===1===1 // false



回答4:


The behaviour that you mentioned is correct.

Its because === implies matching based on type and value. true === 1 does not match on type, but true == 1 matches based on value.




回答5:


if 1==1==1 then it will be true



来源:https://stackoverflow.com/questions/19955573/why-is-1-1-1-false

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!