typeof var
* 适用于检测原始数据类型
* 检测引用类型时,返回值大部分都是 object
* 检测精确度一般
**_desc_**检测数据类型**params**{ any } var**return**{ string } 类型
| 原始数据类型 | 检测结果 |
|---|---|
| Number | number |
| String | string |
| Boolean | boolean |
| Symbol(ES6) | symbol |
| bigint(ES10) | 不清楚 |
| undefined | undefined |
| null | object |
| NaN | number |
| 引用类型 | 检测结果 |
|---|---|
| Array | object |
| Function | function |
| Object | object |
| Set / WeakSet | object |
| Map / WeakMap | object |
| Date | object |
| Math | object |
Object.prototype.toString.call(var)
* 适用于检测除 NaN 以外的所有数据类型
检测的精确度较高*
**_desc_**检测数据类型**params**{ any } var**return**{ string } 类型格式: [object 类型]
| 原始数据类型 | 检测结果 |
|---|---|
| Number | [object Number] |
| String | [object String] |
| Boolean | [object Boolean] |
| Symbol(ES6) | [object Symbol] |
| bigint(ES10) | 不清楚 |
| undefined | [object Undefined] |
| null | [object Null] |
| NaN | [object Number] |
| 引用类型 | 检测结果 |
|---|---|
| Array | [object Array] |
| Function | [object Function] |
| Object | [object Object] |
| Set | [object Set] |
| WeakSet | [object WeakSet] |
| Map | [object Map] |
| WeakMap | [object WeakMap] |
| Date | [object Date] |
| Math | [object Math] |
var instaceof Constructor
* 检测构造函数的 prototype 属性是否出现在某个实例对象的原型链上
*适用于检测引用数据类型**
**_desc_**构造函数检测**params**{ any } var**params**{ object } Constructor 构造函数**return**{ boolean }
"use strict";function Human() {}console.log(new Human() instanceof Human); // trueconsole.log([] instanceof Array); // trueconsole.log(function() {} instanceof Function); // trueconsole.log({} instanceof Object); // trueconsole.log(new Date() instanceof Date); // true
Number.isNaN(var)**
* typeof 和 Object.prototype.toString.call 均无法检测 NaN 数据类型
**_desc_**检测数据类型是否是 NaN**params**{ any } var**return**{ boolean }
"use strict";var num = NaN;console.log(typeof num); // "number"console.log(Object.prototype.toString.call(num)); // "[object Number]"console.log(Number.isNaN(num)); // true
