Null:数组
null是js中的关键字,表示空值,null能够看做是object的一个特殊的值,若是一个object值为空,表示这个对象不是有效对象。函数
Undefined:对象
undefined不是js中的关键字,其是一个全局变量,是Global的一个属性,如下状况会返回undefined:blog
1)使用了一个未定义的变量;var i;内存
2)使用了已定义但未声明的变量;io
3)使用了一个对象属性,但该属性不存在或者未赋值;console
4)调用函数时,该提供的参数没有提供:function
function func(a){ console.log(a); } func();//undefined
5)函数没有返回值时,默认返回undefinedclass
var aa=func(); aa;//undefined
相同点:变量
都是原始类型的值,保存在栈中变量本地
二者的区别:
1.类型不同:
console.log(typeOf undefined);//undefined console.log(typeOf null);//object
2.转化为值时不同:undefined为NaN ,null为0
console.log(Number(undefined));//NaN console.log(Number(10+undefined));//NaN console.log(Number(null));//0 console.log(Number(10+null));//10
3.undefined===null;//false
undefined==null;//true
什么时候使用:
null当使用完一个比较大的对象时,须要对其进行释放内存时,设置为null;
var arr=["aa","bb","cc"]; arr=null;//释放指向数组的引用