undefined与null

undefined 声明的变量尚未初始化

null 对象尚未存在

eg:

var a;

console.log(typeof a);

输出undefined

var b= document.getElementById("b");

console.log(typeof b);

console.log(null== b);

第一行输出object! 这其实是javascript最早的一个错误,但被沿袭了下来...

第二行输出true,因为document.getElementById("b")并不存在

特别注意:

console.log(null== undefined);

输出的结果为true!因为undefined是null的派生

但console.log(null=== undefined);会输出false

原文地址:https://www.cnblogs.com/yanze/p/5990872.html