JS判断数据类型

判断数据的基本类型

1. String类型

console.log(Object.prototype.toString.call('asb'));   // [object String]
console.log(Object.prototype.toString.call('asb')=='[object String]');   // true

通过Object的原生方法可以获取参数的类型,那么就可以判断其是否是这个类型。

2. Number类型

console.log(Object.prototype.toString.call(123)) ;    // [object Number]

3. Boolean类型

console.log(Object.prototype.toString.call(true)) ; // [object Boolean]

4. Array类型

console.log(Object.prototype.toString.call([1,'2','a'])) ; // [object Array]

5. Date类型

console.log(Object.prototype.toString.call(new Date())) ; // [object Date]

6. Function类型

console.log(Object.prototype.toString.call(new Function())) ; // [object Function]

7. Undefined类型

console.log(Object.prototype.toString.call(undefined)) ; // [object Undefined]

8. Null类型

console.log(Object.prototype.toString.call(null)) ; // [object Null]

  

原文地址:https://www.cnblogs.com/zys2019/p/14279141.html