undefined:表示没有数值,在使用var 声明变量但未对其加以初始化时,这个变量的值就是undefined
null:表示有数值,但是数值为“空”.
1.两者的声明
声明但未定义:undefined
var a;
console.log(a);
声明且定义为null;
var b = null;
console.log(b);
2.typeof类型不同
undefined的类型是undefined;null类型是object;
console.log(typeof(undefined));
console.log(typeof(null));
3.转化为数值的类型不同
undefined转化为数值是NaN
console.log(Number(undefined));
console.log(Number(undefined+10));
null转化为数值是0
console.log(Number(null));
console.log(Number(null+10));
4.两者之间 == 和 ===
运算符比较:先判断两侧内容的数据类型,如果数据类型不同,会自动转化为相同类型,再进行比较;
===运算符比较:比较两侧内容的数据类型以及值,如果两侧内容数据类型不同,直接就是false,不会执行数据类型自动转化;
console.log(undefined == null); //true
console.log(undefined === null); //false
逆战班