JS 数据类型判断

来源:https://blog.csdn.net/Raytheon107/article/details/91975043

假设数据是a

1.typeof 返回一个表示数据类型的字符串,返回结果包括:number、boolean、string、symbol、object、undefined、function等7种数据类型,但不能判断null、array等

console.log(typeof(a))

2.instanceof 用来判断A是否为B的实例,A instanceof B, 返回 boolean 值。instanceof 用来测试一个对象在其原型链中是否存在一个构造函数的 prototype 属性,但它不能检测 null 和 undefined

console.log(a instanceof Date)

3.Object.prototype.toString.call() 一般数据类型都能够判断,最准确最常用的一种


Object.prototype.toString.call(a)

原文地址:https://www.cnblogs.com/Alex-Mercer/p/12059168.html