区别:
共同点:都是代表没有值
Null表示没有对象,该处不应该有值。
Undefined表示缺少值,该处应该有值,但是没有定义。
转为数值也不同,null转为数值为0,undefined转为数值为NaN
Console.log(Number(null))
Console.log(Number(undefined))
应用:
什么时候会出现undefined
1 变量声明了,但没有赋值,等于undefined;
Var change;
console.log(change);
2 对象没有赋值的属性,改属性为undefined
Var obj = {} ;
console.log(obj.name)
3 调用函数时,应该提供的参数没有提供,该参数为undefined;
Const fun = (a) =>{
Console.log(a)
}
4 函数没有返回值
var ff = fun1()
console.log(ff)