JavaScript三等于和三变量比较

时间:2013-03-11 05:45:21

标签: javascript triple-equals

有人可以解释一下吗?

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

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

2 个答案:

答案 0 :(得分:7)

这个表达式:

1 === 1 === 1

评估为:

(1 === 1) === 1

评估括号内的表达式后:

true === 1

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

1 === 1 === true

答案 1 :(得分:1)

平等是left-to-right precedence operation

所以:

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

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