- undefined和null两个意思差不多,但还是有差别。
- 在if语句中,undefined和null都被转为false,相等运算符(==)甚至直接报告两者相等。undefined、null、false、0、NaN、空字符串,都会转换为false
if (!undefined) {
console.log('undefined is false');// undefined is false
}
if (!null) {
console.log('null is false');// null is false
}
console.log(undefined == null)//true
var a
if(a){
console.log(a)//undefined
console.log(!a)//true
}
if(typeof b === "undefined"){
console.log('b===undefined')//b===undefined
}
console.log(undefined==null)//true
console.log(undefined===null)//false
- null表示一个空对象,转成数值型为0。undefined是一个表示定义未赋值、调用函数时没有传参、对象中没有赋值的属性、函数没有返回值,转数值型是NaN。
var a=null
console.log(a+5)//5
var b
console.log(b+5)//NaN