首页 > js中null == undefined返回true

js中null == undefined返回true

rt,如何理解这一现象?


这里有一点要解释,当这个逻辑运算符长度为 2 的时候(==, !=),只是判断外在的值是不是一样的,而不会判断类型。如

var a = 1, b = "1";
console.log(a == b);

它输出的结果就是 true。但是如果我们在中间判断的时候再加上一个等号,那么就是严格判断了,需要类型和值都一样的时候才会是 true,否则就是 false。也就是说

var a = 1, b = "1";
console.log(a === b);

的时候,返回的结果就是 false 了,因为 aint 型的,而 b 则是字符串。

详见:http://blog.xcoder.in/node-learning/2013/08/16/node-3-base/#section-1


If x is null and y is undefined, return true

http://stackoverflow.com/questions/16607761/why-null-undefined-is-true-in-javascript


我记得之前看谁说的来着,应该是@寒冬Winter

在Javascript,任何时候"==="都是比"=="更好的选择


要理解这些看起来怪异的问题,方法只有一个:熟悉规范。

你可以参考一下这个问题: http://.com/q/1010000000305997


问题已经解决了,我是来玩的!

null == undefinedtrue 很正常,因为规范里就是这么规定的,一般情况下 null === undefinedfalse,想不想知道啥情况下 null === undefinedtrue 呢……

(function(undefined) {
    console.log(null === undefined);
})(null);
【热门文章】
【热门文章】