JS 中判断数据类型是否为 null、undefined 或 NaN

判断 undefined

var aaa = undefined; 
console.log(typeof(aaa) === "undefined"); // true

判断 null

var aaa = null; 
console.log(!aaa && typeof(aaa)!='undefined' && aaa!=0); // true

判断 NaN

var aaa = 0/0;
console.log(isNaN(aaa));  // true

因为 NaN 是 JavaScript 之中唯一不等于自身的值,所以可以如下判断:

var aaa = 0/0;
console.log(aaa !== aaa);  // true

其他数据类型判断

var a = "abcdef";
var b = 12345;
var c= [1,2,3];
var d = new Date();
var e = function(){ console.log(111); }; 


console.log(Object.prototype.toString.call(a)); // -------> [object String];
console.log(Object.prototype.toString.call(b)); //  -------> [object Number];
console.log(Object.prototype.toString.call(c)); //  -------> [object Array];
console.log(Object.prototype.toString.call(d)); //  -------> [object Date];
console.log(Object.prototype.toString.call(e)); //  -------> [object Function]; 

更多请参考:https://www.cnblogs.com/cckui/p/7524585.html

原文地址:https://www.cnblogs.com/cckui/p/11507711.html