Javascript: Comparing to null - !== vs !=

我怕爱的太早我们不能终老 提交于 2019-11-30 06:23:36

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

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 ;)

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');
}

It is even more simple

var x = null;
    if (x) 6 
    if (!x) 7

the result is

undefined
7
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!