为什么1 === 1 === 1为假? [英] Why is 1===1===1 false?
问题描述
在浏览器控制台中,输入1===1
将得出true
.输入1===1===1
会得出false
.
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
成为
(1 === 1) === 1
计算结果为
true === 1
是false
.
这是正确的吗?如果没有,这种行为的真正原因是什么?
Is this correct? If not, what's the real reason for this behaviour?
推荐答案
是的,您完全正确.在这里,您有两个相等性检查,它们具有相同的运算符优先级.首先进行评估,然后将其结果应用于下一个相等性检查.
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===1
与(1===1)===1
相同,而(1===1)===1
是true===1
也是false
,因为在这里您可以按值及其类型进行检查. 1==1==1
将导致true
,因为它仅按值检查相等性,因此1==1==1
等于(1==1)==1
等于true==1
等于true
.
1===1===1
is 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
.
这篇关于为什么1 === 1 === 1为假?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!