null和undefined的区别
null和undefined都是js中的基本数据类型,null表示为“无”对象 ,undefined表示“无”的原始值
用Number去判断 null为0,undefined为NaN
console.log(Number(null));//0
console.log(Number(undefined));//NaN
undefined
1.已经声明,未赋值
let a;
console.log(a)//undefined
2.对象的某个属性不存在
let obj={}
console.log(obj.a)
3.传参时,少传参数
function fun(a,b){
console.log(a,b)
}
fun(1)//1,undefined
4.函数默认返回值
function fn(){
console.log('hello')
}
console.log(fn())//undefined
null
1.手动释放内存
let obj={}
obj=null //将obj变为垃圾对象,让垃圾回收机制进行回收,相当于手动释放内存