Javascript:比较null - !== vs!= [英] Javascript: Comparing to null - !== vs !=

查看:93
本文介绍了Javascript:比较null - !== vs!=的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,所以我在我的node.js应用程序上工作时在我的Sublime编辑器上安装了Linter。其中一个东西,它捕获说,我应该总是使用!==比较一个对象为null(我通常使用!=)。



所以我改变了。 ..



我有这个场景:

  var x = null; 
if(x!== null)
console.log('x is not equal to null');



当我使用!==时,控制台打印了那行,即使它显然不是真的。当我把它切换回!=它表现正常。



所以我的问题是,为什么是linter告诉我使用!==如果不做什么想要...



我知道我错过了一些东西。






UPDATE
好​​吧,所以可能比我原来想的要复杂一些。在我的实际代码中,我使用了!==与node.js GLOBAL对象。

  console.log '+ GLOBAL.User); 

if(GLOBAL.User!= null)
{
console.log('User is not null');
}

即使GLOBAL.User为null,控制台行也会打印...

也许这个对象是特别的吗?








b $ b

更新2



确定,所以在阅读评论并查看我的代码后, =可能有问题,如果对象是未定义的,而不是null(见这篇文章:)。



所以在我的case ,我的全局变量可以,这取决于什么时候调用这个方法,未定义,null或满的数据。我要回去更新我的代码,以便它永远不会定义,然后!==将一直工作。



感谢您的帮助!






感谢,
David

解决方案

您的 global.User 未定义,而不是 null 。当使用 == 时,它们的计算结果相等,但是 === 类型。 undefined 的类型为 undefined null code>对象



undefined null 非常相似,但它们通常意味着两个非常不同的东西。通常 undefined 是没有赋值的结果,而 null 有一个值,显式设置为无。


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 != ).

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...

I know I am missing something.


UPDATE Ok, 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.

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...

Perhaps this object is special?



Update 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 whats the difference compared to 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

解决方案

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.

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".

这篇关于Javascript:比较null - !== vs!=的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆