javascript 为什么“未定义等于假”返回假?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19277458/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 14:58:26  来源:igfitidea点击:

Why does "undefined equals false" return false?

javascript

提问by abdul raziq

When I compare undefined and null against Boolean false, the statement returns false:

当我将 undefined 和 null 与 Boolean false 进行比较时,该语句返回 false:

undefined == false;
null == false;

It return false. Why?

它返回假。为什么?

回答by My Stack Overfloweth

With the original answer pointing to the spec being deleted, I'd like to provide a link and short excerpt from the spec here.

由于原始答案指向规范被删除,我想在此处提供规范的链接和简短摘录。

http://www.ecma-international.org/ecma-262/5.1/#sec-11.9.3

http://www.ecma-international.org/ecma-262/5.1/#sec-11.9.3

The ECMA spec doc lists the reason that undefined == falsereturns false. Although it does not directly say why this is so, the most important part in answering this question lies in this sentence:

ECMA 规范文档列出了undefined == false返回 false的原因。虽然没有直接说为什么会这样,但回答这个问题最重要的部分在于这句话:

The comparison x == y, where x and y are values, produces true or false.

If we look up the definition for null, we find something like this:

如果我们查找 null 的定义,我们会发现如下内容:

NULL or nil means "no value" or "not applicable".

In Javascript, undefinedis treated the same way. It is without any value. However, false does have a value. It is telling us that something is not so. Whereas undefinedand nullare not supposed to be giving any value to us. Likewise, there is nothing it can convert to for its abstract equality comparison therefore the result would always be false. It is also why null == undefinedreturns true (They are both without any value). It should be noted though that null === undefinedreturns false because of their different types. (Use typeof(null)and typeof(undefined)in a console to check it out)

在 Javascript 中,undefined处理方式相同。它没有任何价值。但是,false 确实有值。它告诉我们有些事情并非如此。而undefinednull不应该被给予我们的任何价值。同样,对于抽象的相等比较,它无法转换为任何内容,因此结果始终为假。这也是null == undefined返回 true 的原因(它们都没有任何价值)。但应该注意的是,null === undefined由于它们的类型不同,返回 false。(在控制台中使用typeof(null)typeof(undefined)进行检查)

What I'm curious of though, is that comparing NaNwith anything at all will alwaysreturn false. Even when comparing it to itself. [NaN == NaNreturns false]

不过,我很好奇的是,NaN与任何东西进行比较总是会返回 false。甚至在与自己进行比较时。[NaN == NaN返回假]

Also, another odd piece of information: [typeof NaNreturns "number"]

另外,另一个奇怪的信息:[typeof NaN返回“数字”]



Strict Equality

严格平等

If possible, you should avoid using the == operator to compare two values. Instead use === to truly see if two values are equal to each other. == gives the illusion that two values really are exactly equal when they may not be by using coercion. Examples:

如果可能,您应该避免使用 == 运算符来比较两个值。而是使用 === 来真正查看两个值是否彼此相等。== 给人一种错觉,即两个值实际上是完全相等的,但它们可能不是通过使用强制转换来实现的。例子:

5 == "5"is true

5 == "5"是真的

5 === "5"is false

5 === "5"是假的

"" == falseis true

"" == false是真的

"" === falseis false

"" === false是假的

0 == falseis true

0 == false是真的

0 === falseis false

0 === false是假的

回答by vp_arth

This is so because it is so. :)

之所以如此,是因为如此。:)

Read the ECMA standards here: https://www.ecma-international.org/ecma-262/5.1/#sec-11.9.3

在此处阅读 ECMA 标准:https: //www.ecma-international.org/ecma-262/5.1/#sec-11.9.3

回答by asantaballa

So undefined really means undefined. Not False, not True, not 0, not empty string. So when you compare undefined to anything, the result is always false, it is notequal to that.

所以未定义真的意味着未定义。不是 False,不是 True,不是 0,不是空字符串。因此,当您将 undefined 与任何内容进行比较时,结果总是错误的,它等于那个。

回答by james_womack

From the incomparable MDN, sponsored by the company of JavaScript's creator.

来自无与伦比的 MDN,由 JavaScript 的创建者公司赞助。

JavaScript provides three different value-comparison operations:

  • strict equality (or "triple equals" or "identity") using ===,
  • loose equality ("double equals") using ==,
  • and Object.is (new in ECMAScript > 6).

The choice of which operation to use depends on what sort of comparison you are looking to perform.

Briefly, double equals will perform a type conversion when comparing two things; triple equals will do the same comparison without type conversion (by simply always returning false if the types differ); and Object.is will behave the same way as triple equals, but with special handling for NaN and -0 and +0 so that the last two are not said to be the same, while Object.is(NaN, NaN) will be true. (Comparing NaN with NaN ordinarily—i.e., using either double equals or triple equals—evaluates to false, because IEEE 754 says so.) Do note that the distinction between these all have to do with their handling of primitives; none of them compares whether the parameters are conceptually similar in structure. For any non-primitive objects x and y which have the same structure but are distinct objects themselves, all of the above forms will evaluate to false.

JavaScript 提供了三种不同的值比较操作:

  • 使用 === 的严格相等(或“三重相等”或“同一性”),
  • 使用 == 的松散相等(“双重相等”),
  • 和 Object.is(ECMAScript 中的新功能 > 6)。

选择使用哪种操作取决于您要执行的比较类型。

简而言之,double equals 将在比较两个事物时执行类型转换;三重相等将在没有类型转换的情况下进行相同的比较(如果类型不同,则总是返回 false);和 Object.is 的行为方式与三重相等,但对 NaN 和 -0 和 +0 进行了特殊处理,因此最后两个不相同,而 Object.is(NaN, NaN) 将为真. (通常将 NaN 与 NaN 进行比较——即使用双等号或三等号——计算结果为 false,因为 IEEE 754 是这样说的。)请注意,这些之间的区别都与它们对原语的处理有关;他们都没有比较参数在结构上在概念上是否相似。对于任何具有相同结构但本身是不同对象的非原始对象 x 和 y,

For a visual overview of the whole picture of equality in JavaScript: https://dorey.github.io/JavaScript-Equality-Table/

有关 JavaScript 中平等全貌的视觉概览:https: //dorey.github.io/JavaScript-Equality-Table/

The truth is, this seemingly "bad" aspect of JavaScript is a source of power when you understand how it works.

事实是,当您了解 JavaScript 的工作原理时,JavaScript 的这个看似“糟糕”的方面是强大的源泉。

回答by Nikhil_k

You question is half, as we compare undefined/ null to any other types. we will have false return. There is no coercion happening, even we are using == operator.

您的问题是一半,因为我们将 undefined/null 与任何其他类型进行比较。我们将得到虚假回报。没有强制发生,即使我们使用 == 操作符。

回答by Tony Hopkinson

Undefined can't = false because it has not been defined so it's not known whether it does or not

Undefined can't = false 因为它没有被定义所以不知道它是否定义

In english it would be like saying Anonymous = Fred or Anonymous != Fred.

在英语中,这就像说 Anonymous = Fred 或 Anonymous != Fred。

It might be Fred, it might be Bill, but at the moment we haven't a clue.

可能是弗雷德,也可能是比尔,但目前我们还没有任何线索。

Similar rule for Null. It's slightly painful to get your head round at first, but it's a very valuable rule, without it all sorts of ambiguities can creep into your designs.

Null 的类似规则。一开始让你的头脑转过来有点痛苦,但这是一个非常有价值的规则,没有它所有的歧义都会潜入你的设计中。

For instance how to tell the difference between something that hasn't been entered (null) or being blank.

例如,如何区分尚未输入的内容(空)或空白的内容。

回答by Viktor Soroka

According to the specification which was mentioned above:

根据上面提到的规范:

If Type(y) is Boolean, return the result of the comparison x == ToNumber(y).

如果 Type(y) 是布尔值,则返回比较结果 x == ToNumber(y)。

Number(undefined) = NaN;

false == NaN // false

Moreover if we change order false == undefined

此外,如果我们改变顺序 false == undefined

If Type(x) is Boolean, return the result of the comparison ToNumber(x) == y.

如果 Type(x) 是布尔值,则返回比较结果 ToNumber(x) == y。

Number(false) = 0;
0 == undefined

There is no rule for this case, so work the default behavior:

这种情况没有规则,因此请使用默认行为:

Return false.

返回假。

回答by Tyler

Undefined is not the same thing as false, falseis a boolean object (which has a value of 0 therefore it is indeed defined).

undefined 与 false 不是一回事,它false是一个布尔对象(它的值为 0,因此它确实被定义了)。

An example:

一个例子:

var my_var;
var defined = (my_var === undefined)
alert(defined);  //prints true.  It is true that my_var is undefined

my_var = 22;
defined = (my_var === undefined)
alert(defined);  //prints false.  my_var is now defined

defined = (false === undefined)
alert(defined);  //prints false, false is defined

defined = (true === undefined)
alert(defined);  //prints false, true is defined