JS 类型判断方法简要汇总
基本类型
-
除
null以外的基本类型:typeof value === '...'- JS 中的基本类型参见 JS 中的基本数据类型。
typeof运算的返回值参见 JS 中 typeof 运算的返回值。
数组
Array.isArray(arr)arr instanceof Arrayarr.constructor === ArrayObject.prototype.toString.call(arr) === '[object Array]'
对象
obj instanceof Objectobj.constructor === ObjectObject.prototype.toString.call(obj) === '[object Object]'
函数
typeof fn === 'function'fn instanceof Functionfn.constructor === FunctionObject.prototype.toString.call(fn) === '[object Function]'
null
obj === nulltypeof obj === 'object' && !obj
NaN
isNaN(number)Number.isNaN(number)