undefined
是个关键字,而null
不是关键字null
的本质是一个“空”的对象,而undefined
其实是windows的一个属性,叫做未定义,它其实是一个已经定义了的属性,只不过这个属性的值叫做未定义。- 当使用typeof 去查找的时候
因此,===比较结果为falsetypeof(undefined)= “undefined” typeof(null) = “object”
null == undefined // true null === undefined // false
- 当共同转换为number时,
null
返回为0,undefined
返回为NaNconsole.log(+null) // 0 console.log(+undefined) // NaN console.log(2 + null) // 2 console.log(1 + undefined) // NaN