简体   繁体   English

JavaScript中的null和undefined之间的区别?

[英]difference between null and undefined in JavaScript?

According to What is the difference between null and undefined in JavaScript? 根据JavaScript中null和undefined的区别是什么? , null and undefined are two different objects (having different types) in Javascript. nullundefined是Javascript中的两个不同对象(具有不同类型)。 But when I try this code 但是,当我尝试这个代码

var a=null;
var b;
alert(a==null);      // expecting true
alert(a==undefined); // expecting false
alert(b==null);      // expecting false
alert(b==undefined); // expecting true

The output of the above code is: 上面代码的输出是:

true
true
true 
true

Now as == only matches the value, I thought that both undefined and null must have the same value. 现在因为==只匹配该值,我认为undefinednull必须具有相同的值。 So I tried: 所以我尝试过:

alert(null) -> gives null alert(null) - >给出null

alert(undefined) -> gives undefined alert(undefined) - >给出undefined

I don't understand how is this possible. 我不明白这是怎么可能的。

Here is the demo . 这是演示

Edit 编辑

I understand that === will give the expected result because undefined and null have different types, but how does type conversion work in Javascript in the case of == ? 我理解===会给出预期的结果,因为undefinednull有不同的类型,但是在==的情况下,类型转换如何在Javascript中工作? Can we do explicit type conversion like we do in Java? 我们可以像在Java中那样进行显式类型转换吗? I would like to apply a manual type conversion on undefined and null . 我想在undefinednull上应用手动类型转换。

You need to use the identity operator === , not the equality operator == . 您需要使用标识运算符=== ,而不是相等运算符== With this change, your code works as expected : 通过此更改,您的代码按预期工作

alert(a===null);      // true
alert(a===undefined); // false
alert(b===null);      // false
alert(b===undefined); // true

The reason the equality operator fails in this case is because it attempts to do a type conversion. 在这种情况下,等于运算符失败的原因是它尝试进行类型转换。 undefined is of type undefined , and null is of type object ; undefined的类型为undefinednull的类型为object ; in attempting to compare the two, Javascript converts both to false , which is why it ends up considering them equal. 在尝试比较两者时,Javascript将两者都转换为false ,这就是为什么它最终认为它们相等。 On the other hand, the identity operator doesn't do a type conversion, and requires the types to be equal to conclude equality. 另一方面,身份运算符不进行类型转换,并且要求类型等于结束相等。

Edit Thanks to @user1600680 for pointing out, the above isn't quite correct; 编辑 感谢@ user1600680指出,上面说的不太正确; the ECMAScript specification defines the null-to-undefined as special case, and equal. ECMAScript规范将null-to-undefined定义为特殊情况,并且相等。 There's no intermediate conversion to false . 没有中间转换为false


A simpler example of type conversion is number-to-string: 一个更简单的类型转换示例是number-to-string:

 console.log(5 == "5"); // true console.log(5 === "5"); // false 

The above answer has a good quote from Douglas Crockford's Javascript: The Good Parts : 以上答案引用了Douglas Crockford的Javascript:The Good Parts

[The "==" operator does] the right thing when the operands are of the same type, but if they are of different types, they attempt to coerce the values. [“==”运算符在操作数相同时是正确的,但如果它们的类型不同,则会尝试强制执行这些值。 the rules by which they do that are complicated and unmemorable. 他们这样做的规则是复杂和不可取的。

If you don't believe that the rules are complicated and unmemorable , a quick look at those rules will disabuse you of that notion. 如果你不相信这些规则是复杂和不合理的 ,那么快速查看这些规则就会使你失去这种观念。

undefined and null have very different semantic meanings. undefinednull具有非常不同的语义含义。

undefined typically means "There wasn't any reply" and null means "There was a reply and that reply was nothing." undefined通常表示“没有任何回复”, null表示“有回复,回复什么都没有。”

For instance, if I created this object: 例如,如果我创建了这个对象:

var gameState = {
  state: loaded,
  lastPlayer: null,
  lastScore: null
};

This doesn't mean "I don't know who the last player was" rather it means "there wasn't a last player." 这并不意味着“我不知道最后一名球员是谁”,而是意味着“没有最后一名球员”。

为了澄清之前的答案, ==这种方式工作的原因是因为,与===不同,它进行类型转换

 var a;
 var b = null;

a is undefined, b is completely null. a未定义,b完全为空。

== is used to compare for equality in a deliberately loose way, that is often useful ==用于以故意松散的方式比较相等性,这通常很有用

alert("3" == 3.0);

That gives us true though they're clearly different - one's a number and one a string. 这给了我们true虽然他们明显不同 - 一个是数字,一个是字符串。

A lot of the time though, this is great. 很多时候,这很棒。

Likewise, a lot of the time don't care if something has no real value because it was undefined, or because it was explicitly set to null. 同样,很多时候不关心某些东西是否没有实际价值,因为它是未定义的,或者因为它被明确设置为null。

While useful sometimes, we do also sometimes need to know the exact type matches as well as the value, so we have === too. 虽然有时很有用,但我们有时也需要知道确切的类型匹配以及值,所以我们也有===

I will like to also say that the undefined is used with the typeof . 我还想说undefinedtypeof The compare must be as: 比较必须如下:

if( typeof(b)=="undefined" ){}

that gives the same results as 这给出了相同的结果

if( b === undefined ){}

and I have include this extra tests on your code http://jsfiddle.net/A89Qj/5/ 我在你的代码http://jsfiddle.net/A89Qj/5/上加入了这些额外的测试

You need to use === instead of ==. 您需要使用===而不是==。 The === operator behaves the same way as the == operator except it does not do any type conversion. ===运算符的行为与==运算符的行为相同,只是它不进行任何类型转换。

typeof undefined is undefined but type of null is object. typeof undefined是未定义的,但null的类型是object。 null === undefined will give you false. null === undefined会给你假。 but null == undefined will give you true. 但是null == undefined会给你真实的。 as boh are of different data type but have same value. 因为boh具有不同的数据类型但具有相同的值。

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

相关问题 null 和 JavaScript 中的 undefined 有什么区别? - What is the difference between null and undefined in JavaScript? 在 javascript 中使用 != null 和 != undefined 之间有什么功能区别吗? - Is there any function difference between using != null and != undefined in javascript? JavaScript 检查 null 与 undefined 以及 == 和 === 之间的区别 - JavaScript checking for null vs. undefined and difference between == and === 传入 null 和 undefined in useRef 之间的区别 - Difference between passing in null and undefined in useRef 为什么 null 是一个对象,null 和 undefined 有什么区别? - Why is null an object and what's the difference between null and undefined? Javascript中未定义和未定义之间的区别 - Difference between undefined and not being defined in Javascript Javascript在'undefined'和'not defined'之间有什么区别? - What is the difference in Javascript between 'undefined' and 'not defined'? JavaScript:`if(!x)`和`if(x == null)`有什么区别? - JavaScript: What is the difference between `if (!x)` and `if (x == null)`? JavaScript中的undefined和window.undefined有什么区别? - What's the difference between undefined and window.undefined in JavaScript? JavaScript:typeof(var)==='undefined' 与 =='undefined' 之间的区别? - JavaScript: difference between typeof(var)==='undefined' vs =='undefined'?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM