为什么1 === 1 === 1为假? [英] Why is 1===1===1 false?

查看:124
本文介绍了为什么1 === 1 === 1为假?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在浏览器控制台中,输入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)===1true===1也是false,因为在这里您可以按值及其类型进行检查. 1==1==1将导致true,因为它仅按值检查相等性,因此1==1==1等于(1==1)==1等于true==1等于true.

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.

这篇关于为什么1 === 1 === 1为假?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆