For those of you who have read the Little Red Book, JavaScript can determine data types in the following ways:

  1. typeof
  2. instanceof
  3. constructor

So let’s summarize the pros and cons of these approaches by defining some initial variables

var num = 123; var str = 'abcdef'; var bool = true; var arr = [1, 2, 3, 4]; var obj = {name:'wenzi', age:25}; var func = function(){ console.log('this is function'); } var und = undefined; var nul = null; var date = new Date(); Var/reg = ^ [a zA - Z] {5, 20} $/; var error= new Error();Copy the code

typeof

Typeof is used to get the typeof a variable or expression – usually to determine the value type

typeof num,  // number
typeof str,  // string
typeof bool,  // boolean
typeof arr,  // object
typeof obj,  // object
typeof func,  // function
typeof und,  // undefined
typeof nul,  // object
typeof date,  // object
typeof reg,  // object
typeof error // object
Copy the code

Arr, JSON, NUl, date, reg, error are all detected as object, and other variables can be correctly detected. If a variable is of type number, string, Boolean, function, or undefined, use typeof. Other reference types and null are not typed

instanceof

The instanceof operator is used to determine whether an object is a property of its prototype constructor in its prototype chain, and therefore makes sense when comparing objects (reference types)

num instanceof Number, // false str instanceof String, // false bool instanceof Boolean, // false arr instanceof Array, // true-- arr instanceof Object -- obj instanceof Object, // true-- func instanceof Function, // true und instanceof Object, // false nul instanceof Object, // false date instanceof Date, // true reg instanceof RegExp, // true error instanceof Error // trueCopy the code

It is important to note that arr and obj are both true when instanceof Object is used, which makes it inaccurate to determine the value of an Object

constructor

Constructor is originally a property on the prototype object that points to the constructor. However, depending on the order in which the instance object looks for properties, if there are no instance properties or methods on the instance object, the constructor property can be used for instance objects as well

function Person(){} var Tom = new Person(); Console. log(tom.constructor ==Person, num. Constructor ==Number, str.constructor==String, obj.constructor==Boolean, arr.constructor==Array, json.constructor==Object, func.constructor==Function, date.constructor==Date, reg.constructor==RegExp, error.constructor==Error ); // All results are trueCopy the code

It looks perfect on the surface, but there are two disadvantages:

  1. Undefined and null have no constructor property“, so when judgingThe code may report errorsThis is fatal and causes the code to fail, so only use it if you are sure that the values in question are not undefined and null
  2. Due to theThe constructor property can be changedCan also lead to the detection of incorrect results

Universal perfect judgment method

    Object.prototype.toString.call(num),  // '[object Number]'
    Object.prototype.toString.call(str),  // '[object String]'
    Object.prototype.toString.call(bool),  // '[object Boolean]'
    Object.prototype.toString.call(arr),  // '[object Array]'
    Object.prototype.toString.call(obj),  // '[object Object]'
    Object.prototype.toString.call(func),  // '[object Function]'
    Object.prototype.toString.call(und),  // '[object Undefined]'
    Object.prototype.toString.call(nul),  // '[object Null]'
    Object.prototype.toString.call(date),  // '[object Date]'
    Object.prototype.toString.call(reg),  // '[object RegExp]'
    Object.prototype.toString.call(error)  // '[object Error]'
Copy the code

The ECMA specification defines the Object. The prototype. ToString behavior: First, we take an internal property of the object [[Class]] and return a string similar to “[object Array]” based on this property. Using this method, in conjunction with call, we can take the internal attribute [[Class]] of any object and turn type detection into string comparison for our purposes.

Give it a like if you think it's helpful