简体   繁体   English

为什么 null == undefined 评估为真?

[英]Why does null == undefined evaluate to true?

I have a code below which is not clear to me我下面有一个代码,我不清楚

var a = null; 
if(a==undefined)
alert("true");
else 
alert("false");

When I ran above code it alerts true.当我运行上面的代码时,它会警告为真。

Can anybody explain whats the reason or concept behind this?任何人都可以解释这背后的原因或概念是什么?

It's true because == is the loose equality operator, and null and undefined are loosely equal ( null == undefined is true).这是真的,因为==松散相等运算符,而nullundefined松散相等( null == undefined为真)。 If you use the strict equality operator, === , they are not equal ( null === undefined is false).如果您使用严格相等运算符=== ,则它们不相等( null === undefined为假)。

Basically, the loose equality operator will coerce its operands if they're of different types (see Abtract Equality Comparison in the spec).基本上,如果它们的类型不同,松散相等运算符将强制其操作数(请参阅规范中的抽象相等比较)。 0 == "" is true, for instance, because if you coerce "" to a number, it's 0 .例如, 0 == ""是真的,因为如果你将""强制转换为一个数字,它就是0 The strict equality operator considers operands of different types not equal (see Strict Equality Comparison );严格相等运算符认为不同类型的操作数不相等(请参阅严格相等比较); it does not coerce.它不强迫。


On browsers, there's a third value that's == to null and undefined : document.all .在浏览器上,还有第三个值==nullundefineddocument.all document.all behaves like undefined in various specification operations. document.all在各种规范操作中表现得像undefined This is so that really, really old code that was using if (document.all) or similar to go an IE-specific direction doesn't do that on modern browsers, since the features that they were avoiding exit on IE versions that handle document.all this way.如此一来,使用if (document.all)或类似代码来实现特定于 IE 的方向的非常非常旧的代码在现代浏览器上不会这样做,因为它们避免在处理document.all IE 版本上退出的功能document.all这条路。 This is defined in the spec .这是在规范中定义的

==运算符的规范被定义为null == undefined返回true

The spec - Clause 11.9.3 .规范 -第 11.9.3 条 See clauses 2 and 3.见第 2 条和第 3 条。

undefined means a variable has not been defined, or given a value. undefined意味着一个变量没有被定义,或者没有被赋予一个值。 null is a special object within javascript . nulljavascript一个特殊对象

Comparing the values using equals == will always give you true because they are both basically the same in value (0, nothing, nada, zip) .使用 equals ==比较值将始终为您提供true因为它们的值基本相同(0, nothing, nada, zip)

Using strict equals === though, will return false because null is an object and undefined is just a blank variable .但是,使用严格等于===将返回false因为 null 是一个objectundefined只是一个空白variable

If a variable is pointing to nothing, it's null.如果变量不指向任何内容,则它为空。 That's similar in concept to an undefined variable, which has not yet been initialized.这在概念上类似于尚未初始化的未定义变量。

Javascript interpreted this to make loose equality (==) treat null and undefined the same way. Javascript 将此解释为使松散相等 (==) 以相同的方式处理 null 和 undefined。 Strict equality (===) is more picky;严格相等(===)比较挑剔; if you have different data types such as null and undefined, they won't be equal.如果您有不同的数据类型,例如 null 和 undefined,它们将不相等。

See What is the difference between null and undefined in JavaScript?请参阅JavaScript 中的 null 和 undefined 有什么区别? on differences between undefined and null and Which equals operator (== vs ===) should be used in JavaScript comparisons?关于 undefined 和 null 之间的差异以及JavaScript 比较中应该使用哪个等于运算符 (== vs ===)?

This behavior stems from JavaScript being a loosely-typed language.这种行为源于 JavaScript 是一种松散类型的语言。 In the case of equality, this means values of differing data types can be compared.在相等的情况下,这意味着可以比较不同数据类型的值。

Both null and undefined are considered "falsy", therefore they're loosely considered equal. nullundefined都被认为是“假的”,因此它们被粗略地认为是相等的。

Logically, if null == false and undefined == false , then null == undefined .从逻辑上讲,如果null == falseundefined == false ,则null == undefined

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

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