Comparing to null - !== vs != in JavaScript

后端 未结 3 1366
南方客
南方客 2020-12-29 20:27

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 nu

相关标签:
3条回答
  • 2020-12-29 20:58

    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');
    }
    
    0 讨论(0)
  • 2020-12-29 21:18

    It is even more simple

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

    the result is

    undefined
    7
    
    0 讨论(0)
  • 2020-12-29 21:25

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

    0 讨论(0)
提交回复
热议问题