简体   繁体   English

JavaScript三等于和三变量比较

[英]JavaScript triple equals and three-variable comparison

Can somebody explain this? 有人可以解释一下吗?

1 == 1        //true, as expected
1 === 1       //true, as expected
1 == 1 == 1   //true, as expected
1 == 1 == 2   //false, as expected
1 === 1 === 2 //false, as expected
1 === 1 === 1 //false? <--

Also is there a name for boolean logic that compares more than two numbers in this way (I called it "three-variable comparison" but I think that'd be wrong...) 还有一个布尔逻辑的名称,以这种方式比较两个以上的数字(我称之为“三变量比较”,但我认为这是错误的......)

This expression: 这个表达式:

1 === 1 === 1

Is evaluated as: 被评估为:

(1 === 1) === 1

After evaluating the expression inside parentheses: 在评估括号内的表达式后:

true === 1

And that expression is logically false. 而且这个表达在逻辑上是错误的。 The below expression returns true as expected though: 下面的表达式按预期返回true

1 === 1 === true

Equality is a left-to-right precedence operation . 平等是一种从左到右的优先操作

So: 所以:

1 == 1 == 1
true == 1
true

And: 和:

1 === 1 === 1
true === 1
false // because triple-equals checks type as well

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM