Several ways to judge variable type in JavaScript | typeof | instanceof | constructor | Object.prototype.toString.call()

Several ways to judge variable type in JavaScript

Method 1: typeof

  • It is often used to judge basic data types. For reference data types , everything else is typeof functionreturned .'function''object'

  • and typeof null also returns'object'

    • Reason: When JavaScript stores variables at the bottom layer, it will store the type information of the variable in the 1-3 bits of the machine code of the variable. The 1-3 bits of the object type are , 000and nullthe machine code is 全0, so it will also be judged as an object typeofduring detection .null

Method 1 code example:

let num = 123
let str = 'hello'
let bool = true
let s = Symbol('hello')
let obj = {
    
    a:'hi'}
let arr = ['a','b','c']
let fun = function(){
    
    
    console.log('hello')
}
console.log('typeof num '+ typeof num) 
console.log('typeof str '+ typeof str)
console.log('typeof bool '+ typeof bool)
console.log('typeof null '+ typeof null)
console.log('typeof undefined '+ typeof undefined)
console.log('typeof s '+ typeof s)
console.log('typeof obj '+ typeof obj)
console.log('typeof arr ' + typeof arr)
console.log('typeof fun ' + typeof fun)

operation result:

image-20221107231714532

Method 2: instanceof

  • instanceofJudgment method: if a instanceof B

    • Returns true if Bthe function's explicit prototype object prototypeis on a's implicit prototype chain, otherwise returns false.
  • It cannot be used to detect simple data types. The detection process is cumbersome and null, undefined, and symbols in simple data types cannot be detected.

Method 2 code example:

let num = 123
let str = 'hello'
let bool = true
let s = Symbol('hello')
let obj = {
    
    a:'hi'}
let arr = ['a','b','c']
let fun = function(){
    
    
    console.log('hello')
}
console.log(num instanceof Number) //false
console.log(str instanceof String)  //false
console.log(bool instanceof Boolean )//false
console.log(undefined instanceof Object)//false
console.log(null instanceof Object) //false
console.log(s instanceof Symbol ) //false
console.log(obj instanceof Object ) //true
console.log(arr instanceof Array)  //true
console.log(fun instanceof Function )  //true

operation result:

image-20221107234504050

Method 3: constructor

  • Each function has a prototypeproperty, which points to an empty objectobject by default, called the prototype object of the function. There is a property on this prototype object called constructor, which points to the function object by default. When an instance object is used to obtain constructoran attribute, if it cannot be found on its own attribute, it will be searched along the prototype chain, and the found constructorattribute points to the constructor of the instance object. Therefore, the reference data type can constructorbe judged by the attribute.

Method 3 code example:

let num = 123
let str = 'hello'
let bool = true
let s = Symbol('hello')
let obj = {
    
    a:'hi'}
let arr = ['a','b','c']
let fun = function(){
    
    
    console.log('hello')
}
console.log(num.constructor === Number) //true
console.log(str.constructor === String) //true
console.log(bool.constructor === Boolean)//true
console.log(s.constructor === Symbol) //true
console.log(obj.constructor === Object) //true
console.log(arr.constructor === Array) // true
console.log(fun.constructor === Function) //true

operation result:

image-20221107235825911

Method 4: Object.prototype.toString.call()

  • Each instance of Object has a toString method. This method is on the prototype of the Object constructor. The Object.prototype.toString method will output the string representation of the object: [Object type]
    • The Object.prototype.toString() method is the toString method on the prototype of the Object constructor. Only this method will output the string representation of the object that calls the method. Other data types have basically rewritten the toString method (see the comparison in the code example below), so if you directly call the toString method through the instance object, you cannot get the Object.prototype.toString() method, and you can only call it through Object.prototype.toString.call()/apply() to realize the judgment of the data type.
  • Among these four methods, Object.prototype.toString.call() is the one that can detect various data types and detect them accurately. Including null and undefined can be accurately detected.

Method 4 code example:

let num = 123
let str = 'hello'
let bool = true
let s = Symbol('hello')
let obj = {
    
    a:'hi'}
let arr = ['a','b','c']
let fun = function(){
    
    
    console.log('hello')
}
console.log(num.toString())
console.log(Object.prototype.toString.call(num))
console.log(str.toString())
console.log(Object.prototype.toString.call(str))
console.log(bool.toString())
console.log(Object.prototype.toString.call(bool))
console.log(s.toString())
console.log(Object.prototype.toString.call(s))
console.log(obj.toString())
console.log(Object.prototype.toString.call(obj))
console.log(arr.toString())
console.log(Object.prototype.toString.call(arr))
console.log(fun.toString())
console.log(Object.prototype.toString.call(fun))
console.log(Object.prototype.toString.call(null))
console.log(Object.prototype.toString.call(undefined))

operation result:

image-20221108002523018

Summarize

The above four methods are the four methods for judging variable data types in JavaScript. If there are any deficiencies in the code or ideas, welcome to add.

Guess you like

Origin blog.csdn.net/dxy1128/article/details/127742473