4

有人可以解释一下吗?

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? <--

还有一个布尔逻辑的名称,它以这种方式比较两个以上的数字(我称之为“三变量比较”,但我认为那是错误的......)

4

2 回答 2

7

这个表达式:

1 === 1 === 1

被评估为:

(1 === 1) === 1

在评估括号内的表达式后:

true === 1

而这个表达在逻辑上是错误的。以下表达式true按预期返回:

1 === 1 === true
于 2013-03-11T05:48:34.923 回答
1

相等是从左到右的优先运算

所以:

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

和:

1 === 1 === 1
true === 1
false // because triple-equals checks type as well
于 2013-03-11T05:49:53.730 回答