null:用来表示尚未存在的对象,常用来表示函数企图返回一个不存在的对象(引用为空)。
undefined:当声明的变量还未初始化时,变量的默认值为undefined(只是定义了引用)。
var a;
console.log(a == undefined) //true
null和undefined的区别
- null he undefined 都只有一个值
- typeof null 为 object
- typeof undefined 为 undefined
- null == undefined 为true (因为undefined是null派生出来的)
- null === undefined 为false (因为值相同类型不同)
可以用null和undefined来清除属性
区分JS中的undefined,null,"",0和false
alert(0 == ''); //true
alert(0 == false); //true
alert(false == ''); //true
alert(null == undefined); //true
alert(!0); //true
alert(!false); //true
alert(!undefined); //true
alert(!null); //true
alert(!''); //true
alert(0 == undefined); //false
alert(0 == null); //false
alert(false == null); //false
alert(false == undefined);//false
alert('' == null); //false
alert('' == undefined); //false
在js做比较的时候,有这样的三条规则:
- 如果比较的两者中有bool,会把 bool 先转换为对应的 number,即 0 和 1
- 如果比较的双方中有一方为number一方为string,会把string转换为数字
- 把string直接转换为bool的时候,空字符串‘'转换为 false,除此外的一切字符串转换为 true