Null:
null是一种特殊的对象,即一个空对象指针,出现在某个变量可以用object表示但是没有object关联 ,这也就是为什么typeof null运算的结果是object。
Undefined:
一个变量声明了但是没有赋值,没有声明和声明了没有定义的变量typeof的结果都是undefined,原因是:虽然这两种变量从技术角度看有本质区别,但实际上无论对哪种变量也不可能执行真正的操作 ——摘自《JavaScript高级程序设计(第3版)》。所以在使用时,声明了的变量尽量赋值,如果是空对象则为null,其他根据变量类型进行赋值。
实际上,undefined 值是派生自 null 值的,因此 ECMA-262 规定对它们的相等性测试要返回 true。 ——摘自《JavaScript高级程序设计(第3版)》
null == undefined; // true
null === undefined; //false
void 0:
void 0 === void(0) === undefined
当我们在写a标签的时候,经常会用到这种写法(摘自MDN):
<a href="javascript:void(0);">
Click here to do nothing
</a>
<a href="javascript:void(document.body.style.backgroundColor='green');">
Click here for green background
</a>
在ES5之前,window下的undefined是可以被重写的,于是导致了某些极端情况下使用undefined会出现一定的差错。 所以,用void 0是为了防止undefined被重写而出现判断不准确的情况。 注: ES5之后的标准中,规定了全局变量下的undefined值为只读,不可改写的,但是局部变量中依然可以对之进行改写。 补充一下:非严格模式下,undefined是可以重写的,严格模式则不能重写。
typeof null // "object" (not "null" for legacy reasons)
typeof undefined // "undefined"
null === undefined // false
null == undefined // true
null === null // true
null == null // true
!null // true
isNaN(1 + null) // false
isNaN(1 + undefined) // true