为什么“0 === -0”在JavaScript中是真的吗? [英] Why is "0 === -0" true in JavaScript?

查看:404
本文介绍了为什么“0 === -0”在JavaScript中是真的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近有关 http://wtfjs.com/ 的文章。作者在没有解释的情况下写下以下内容。

In a recent post on http://wtfjs.com/. An author writes following without explanation which happens to be true.

0 === -0 //returns true

我对===运算符的理解是,如果操作数指向同一个对象,则返回true。

My understanding about === operator is it returns true if operands point to same object.

此外, - 运算符返回对操作数负值的引用。根据这个规则,0和-0不应该相同。

Also, - operator returns a reference to negative value of operand. With this rule, 0 and -0 should not be the same.

那么,为什么0 === -0?

So, why is 0 === -0 ?

推荐答案

实际上,即使在位级别,0和-0也不相同。但是,有一个特殊情况为+/- 0实现,因此它们相等。

In fact, 0 and -0 are not the same even at the bit level. However, there is a special case implemented for +/-0 so they compare as equal.

===运算符在应用于原始数字时按值进行比较。

The === operator compares by value when applied to primitive numbers.

这篇关于为什么“0 === -0”在JavaScript中是真的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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