根据What is the difference between null and undefined in JavaScript?的说法,null
和undefined
是Javascript中的两个不同的对象(具有不同的类型)。但是当我尝试这段代码时
var a=null;
var b;
alert(a==null); // expecting true
alert(a==undefined); // expecting false
alert(b==null); // expecting false
alert(b==undefined); // expecting true
上述代码的输出为:
true
true
true
true
现在,因为==
只与值匹配,所以我认为undefined
和null
必须具有相同的值。所以我试着:
alert(null)
->给null
alert(undefined)
->给undefined
我不明白这怎么可能。
这是the demo。
编辑
我知道===
会给出预期的结果,因为undefined
和null
有不同的类型,但是在==
的情况下,类型转换是如何工作的呢?我们可以像在Java中那样进行显式类型转换吗?我想在undefined
和null
上应用手动类型转换。
https://stackoverflow.com/questions/12218483
复制相似问题