Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

为什么 null >= 0 是 true,但是 null == 0 是 false (隐式类型转换) #91

Open
lovelmh13 opened this issue Jun 25, 2021 · 0 comments

Comments

@lovelmh13
Copy link
Owner

== 的比较过程

image
image

总结一下就是:
能转数字的最后都转成数字,对象用 ToPrimitive 转成原始类型,两边进行比较。
但是注意,按照上面的规则,undefined 和 null 只有在这俩比较的时候,undefined == null // true,不会变成 0。

undefined 和 null 在进行相等判断的时候,不会进行类型转换!

所以,null == 0undefined == 0 都是 false

>=

进行大于等于比较的时候,undefined 和 null 进行了类型转换,变成了 0,所以 null >= 0 是 true。

null > 0 是 false,同理,null 转化为了 0,0 > 0 是 false

@lovelmh13 lovelmh13 changed the title 为什么 null >= 0 是 true,但是 null == 0 是 false 为什么 null >= 0 是 true,但是 null == 0 是 false (隐式类型转换) Jun 25, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant