Comparison and difference between null and undefined in javascript

Undefined and null in JavaScript

Directly upload code and screenshots

//在javascript中null和undefined的比较和区别

//true false
console.log(undefined == null, undefined === null);
//null可以自动转为0
//null 5 10
console.log(null, null + 5, 2 + null + 8);

//null知识点
//null属于什么数据类型
console.log("null的数据类型为" + typeof null);//object
console.log("Number(null)的数据类型为" + typeof Number(null));//number
console.log(null == 0, Number(null) == 0);//false true
console.log(null < 1, null > -1);//true true
//如下的代码就奇怪了,null == 0这句话中,null没有自动转为0
console.log(null == 0, null === 0); //false false
//false false false false
console.log(null > 0, null < 0, null == 0, null === 0);

if(!null) {
	console.log("null is false");
}

//undefined知识点
//undefined属于什么数据类型
console.log("undefined的数据类型为" + typeof undefined);//undefined
console.log(Number(undefined));//NaN

if(!undefined) {
	console.log("undefined is false");
}

The results of the operation are as follows:

Guess you like

Origin blog.csdn.net/czh500/article/details/114495478