简体   繁体   English

为什么“undefined equals false”返回 false?

[英]Why does "undefined equals false" return false?

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?为什么?

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 == false returns false. ECMA规范文档列出了undefined == false返回false的原因。 Although it does not directly say why this is so, the most important part in answering this question lies in this sentence: 虽然它没有直接说明为什么会这样,但回答这个问题最重要的部分在于这句话:

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, undefined is treated the same way. 在Javascript中, undefined以相同的方式处理。 It is without any value. 它没有任何价值。 However, false does have a value. 但是,false确实有价值。 It is telling us that something is not so. 它告诉我们事情并非如此。 Whereas undefined and null are not supposed to be giving any value to us. undefinednull不应该给我们任何价值。 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 == undefined returns true (They are both without any value). 这也是null == undefined返回true的原因(它们都没有任何值)。 It should be noted though that null === undefined returns false because of their different types. 应该注意的是, null === undefined因为它们的类型不同而返回false。 (Use typeof(null) and typeof(undefined) in a console to check it out) (在控制台中使用typeof(null)typeof(undefined)来检查它)

What I'm curious of though, is that comparing NaN with anything at all will always return false. 但我很好奇的是,将NaN与任何东西进行比较总是会返回错误。 Even when comparing it to itself. 即使将它与自身进行比较。 [ NaN == NaN returns false] [ NaN == NaN返回false]

Also, another odd piece of information: [ typeof NaN returns "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"是假的

"" == false is true "" == false是真的

"" === false is false "" === false是假的

0 == false is true 0 == false为真

0 === false is false 0 === false为false

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

So undefined really means undefined. 所以undefined真的意味着未定义。 Not False, not True, not 0, not empty string. 不是假,不是真,不是0,不是空字符串。 So when you compare undefined to anything , the result is always false, it is not equal to that. 因此,当您将undefined与任何内容进行比较时,结果始终为false,它等于该值。

From the incomparable MDN, sponsored by the company of JavaScript's creator. 来自无与伦比的MDN,由JavaScript的创建者公司赞助。

JavaScript provides three different value-comparison operations: JavaScript提供三种不同的值比较操作:

  • strict equality (or "triple equals" or "identity") using ===, 严格平等(或“三等于”或“身份”)使用===,
  • loose equality ("double equals") using ==, 松散的平等(“双等于”)使用==,
  • and Object.is (new in ECMAScript > 6). 和Object.is(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; 简而言之,double equals在比较两件事时会执行类型转换; triple equals will do the same comparison without type conversion (by simply always returning false if the types differ); 如果没有类型转换,三等于将进行相同的比较(如果类型不同,则总是返回false); 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. 和Object.is的行为方式与triple equals相同,但是对NaN和-0以及+0进行特殊处理,以便最后两个不相同,而Object.is(NaN,NaN)将为true 。 (Comparing NaN with NaN ordinarily—ie, 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; (通常将NaN与NaN进行比较 - 即,使用double equals或triple equals-评估为false,因为IEEE 754这样说。)请注意,这些之间的区别都与它们对原语的处理有关; 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. 对于具有相同结构但本身是不同对象的任何非原始对象x和y,所有上述形式将评估为false。

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的这个看似“坏”的方面是一种力量源泉。

Undefined can't = false because it has not been defined so it's not known whether it does or not Undefined不能= 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. 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. 首先让你的头部变得有点痛苦,但这是一个非常有价值的规则,如果没有它,各种各样的模糊可能会蔓延到你的设计中。

For instance how to tell the difference between something that hasn't been entered (null) or being blank. 例如,如何区分尚未输入(空)或空白的内容之间的区别。

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. 返回false。

You question is half, as we compare undefined/ null to any other types. 你的问题是一半,因为我们将undefined / null与任何其他类型进行比较。 we will have false return. 我们会有错误的回报。 There is no coercion happening, even we are using == operator. 即使我们使用==运算符,也没有强制发生。

Undefined is not the same thing as false, false is a boolean object (which has a value of 0 therefore it is indeed defined). 未定义与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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM