与 null 比较 - !== vs != 在 JavaScript 中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16204840/
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
Comparing to null - !== vs != in JavaScript
提问by David
Ok, so I installed Linter on my Sublime editor while working on my node.js app. One of the things that it caught said that I should always use !== to compare an object to null (I usually use != ).
好的,所以我在我的 node.js 应用程序上工作时在我的 Sublime 编辑器上安装了 Linter。它捕获的一件事说我应该始终使用 !== 将对象与 null 进行比较(我通常使用 != )。
So I changed it...but then I noticed that the !== wasn't working.
所以我改变了它......但后来我注意到 !== 不起作用。
I have this scenario:
我有这个场景:
var x = null;
if (x !== null)
console.log('x is not equal to null');
When I use the !== the console printed that line even though it was obviously not true. When I switched it back to != it behaved normally.
当我使用 !== 时,控制台打印了该行,即使它显然不是真的。当我将其切换回 != 时,它表现正常。
So my question is, why is linter telling me to use !== if it doesn't do what I want it to...
所以我的问题是,为什么 linter 告诉我使用 !== 如果它没有做我想要的......
I know I am missing something.
我知道我错过了一些东西。
UPDATEOk, so it may be a bit more complicated than I originally thought. In my real code I was using !== with the node.js GLOBAL object.
更新好的,所以它可能比我原先想象的要复杂一些。在我的实际代码中,我将 !== 与 node.js GLOBAL 对象一起使用。
console.log('Global User: ' + GLOBAL.User);
if (GLOBAL.User != null)
{
console.log('User is not null');
}
The console line prints even when GLOBAL.User is null...
即使 GLOBAL.User 为空,控制台行也会打印...
Perhaps this object is special?
也许这个对象很特别?
Update 2
更新 2
Ok, so after reading through the comments and looking at my code, I have learned that !== can have issues if the object is undefined rather than null (see this post: Why is null an object and what's the difference between null and undefined?).
好的,所以在阅读评论并查看我的代码后,我了解到 !== 如果对象未定义而不是 null 可能会出现问题(请参阅这篇文章:为什么 null 是一个对象以及 null 和 undefined 之间的区别是什么?)。
So in my case, my global variable could be, depending on when this method is called, undefined, null, or full of data. I am going to go back and update my code so that it is never undefined and then !== will work consistently.
因此,在我的情况下,我的全局变量可能是未定义的、空的或充满数据的,具体取决于调用此方法的时间。我将返回并更新我的代码,以便它永远不会未定义,然后 !== 将始终如一地工作。
Thanks for the help!
谢谢您的帮助!
Thanks, David
谢谢,大卫
回答by loganfsmyth
Your global.User
is undefined
, not null
. When using ==
they evaluate to equal, but with ===
the items you are comparing need to be the same type. undefined
has the type undefined
and null
has the type object
.
你的global.User
是undefined
,不是null
。使用时,==
它们评估为相等,但与===
您比较的项目需要是相同的类型。undefined
有类型undefined
并且null
有类型object
。
undefined
and null
are very similar, but they generally mean two very different things. Usually undefined
is the result when something has had no value assigned to it, whereas null
has a value, and the value is explicitly set to "nothing".
undefined
和null
非常相似,但它们通常意味着两个非常不同的东西。通常undefined
是当某物没有分配给它的值时的结果,而null
有一个值,并且该值显式设置为“无”。
回答by plalx
The only value that doesn't equal itself in JavaScript is NaN
. If null === null
is false
, then your JavaScript engine has serious problems ;)
JavaScript 中唯一不等于自身的值是NaN
. 如果null === null
是false
,那么您的 JavaScript 引擎存在严重问题;)
To make sure your conditional statement is well written, always use the braces.
为了确保您的条件语句写得很好,请始终使用大括号。
var x = null;
if (x !== null) {
console.log('x is not equal to null');
}
回答by oshribr
It is even more simple
更简单
var x = null;
if (x) 6
if (!x) 7
the result is
结果是
undefined
7