Several ways to judge variable type in JavaScript
Table of contents
Method 1: typeof
-
It is often used to judge basic data types. For reference data types , everything else is
typeof function
returned .'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 ,
000
andnull
the machine code is全0
, so it will also be judged as an objecttypeof
during detection .null
- 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 ,
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:
Method 2: instanceof
-
instanceof
Judgment method: ifa instanceof B
- Returns true if
B
the function's explicit prototype objectprototype
is on a's implicit prototype chain, otherwise returns false.
- Returns true if
-
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:
Method 3: constructor
- Each function has a
prototype
property, which points to an emptyobject
object by default, called the prototype object of the function. There is a property on this prototype object calledconstructor
, which points to the function object by default. When an instance object is used to obtainconstructor
an attribute, if it cannot be found on its own attribute, it will be searched along the prototype chain, and the foundconstructor
attribute points to the constructor of the instance object. Therefore, the reference data type canconstructor
be 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:
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:
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.