1.null,特指对象的值未设置,常用在释放对象,比如拖拽事件,拖拽后需要把 mousedown mousemove mouseup释放,也就是设置为null,其类型检测如下:
var bonly=null;
console.log(bonly); // null
console.log(typeof bonly); // object
var bonly={};
console.log(bonly); // 只有__proto__属性,指向Object
console.log(typeof bonly); // object
2.undefined,表示的是未定义,以下3种情况会出现undefined
console.log(bonly); // 在变量声明前调用,输出为undefined
var bonly;
console.log(bonly); // 变量未定义,输出为undefined
console.log(typeof bonly); // 所以检测类型,输出也为undefined
3.'',""," ",都表示空值
var bonly1='';
var bonly2="";
var bonly3=" ";
console.log(bonly1); // 什么也不输出
console.log(bonly2); // 什么也不输出
console.log(bonly3); // 什么也不输出
console.log(typeof bonly1); // string
console.log(typeof bonly2); // string
console.log(typeof bonly3); // string
4.isNaN 判断参数是否不是数组,如果是数字返回false,不是数字返回true,注意的是isNaN函数会首先尝试将这个参数转换为数值,然后才会对转换后的结果是否是NaN进行判断。去看MDN解释
console.log(isNan(1));
console.log(isNaN("1")); // false
console.log(isNaN("1n")); // true
5.null与undefined的区别
console.log(typeof null); // object (因为一些以前的原因而不是'null')
console.log(typeof undefined); // "undefined"
console.log(null === undefined); // false
console.log(null == undefined); // true
console.log(null === null); // true
console.log(null == null); // true
console.log(!null); // true
console.log(isNaN(1 + null)); // false
console.log(isNaN(1 + undefined)); // true
6.''和""判断问题
console.log(''===""); // true
console.log(''===" "); // false
console.log(null===""); // false
console.log(undefined===""); // false
console.log(undefined===undefined); // true