JS数据类型判断方式
- typeof
基本类型:
number string boolean undefined null symbol
typeof null // “object” 除了null以外其余的都能正确返回
typeof NaN // “number”
引用类型 :
object array map set date regexp
typeof // “object”
typeof function // “function” - instanceof
判断一个引用类型是否是指定类的实例
基本类型不能判断
let a = 0,
b = Number(0),
c = new Number(0);
a instanceof Number // false
b instanceof Number // false
c instanceof Number // true
- Object.prototype.toString.call
准确判断所有数据类型 - 数组还可以通过Array.isArray判断