目录
数据类型比较
undefined和null为JS中的基本数据类型,NaN表示Not A Number!
看代码如下
console.log(typeof undefined);
console.log(typeof null);
console.log(typeof NaN);
定义
null的类型是个一对象,代表一个空值。
undefined的类型是一个对象,代表没有定义。
NaN的类型是一个number,代表不是数值类型。
nudefined
表示未定义、通常出现了声明变量但是没有进行赋值所以会出现undefined
var a;
console.log(a);//undefined
null
1)把一个变量的值设置为null,就表示该变量的值不是有效的对象,数组,数字,字符串和布尔值。null对应类型Object,布尔值false,数字0,字符串“null”
2)null表示变量值为空,可以通过将null赋给变量来清空变量。(可以有利于垃圾回收)
NaN
NaN不多说了直接上代码
// Number类型
console.log(typeof NaN === "number"); // "true"
// 自己和自己并不相等
console.log(NaN === NaN); // "false"
比较
null == undefined 返回true
null===undefined 返回false