Home  >  Article  >  Web Front-end  >  How to determine the data type of an object in javascript?

How to determine the data type of an object in javascript?

不言
不言forward
2018-10-27 15:23:561967browse

The content of this article is about how to determine the data type of an object in JavaScript? , has certain reference value, friends in need can refer to it, I hope it will be helpful to you.

Summary of Boolean value types of different data in js: false: empty string; null; undefined; 0; NaN. true: Except for the above false case, everything else is true;

There are six data types in javascript: string;boolean;Array;Object;null;undefined. How to detect these data types, the summary method is as follows:

The first method: typeof

var obj = {'name':'Tom'}
var arr = ['a','b','c']
var str = 'chenxinming'
var bool = true
var num = 1
var n = null
var fn = function(n){
        console.log(n)
    }
	
console.log(typeof obj)    // object
console.log(typeof arr)    // object
console.log(typeof str)    // string
console.log(typeof bool)    // boolean
console.log(typeof num)    // number
console.log(typeof n)    // object
console.log(typeof fn)    // function

Through the above test, it is found that typeof returns both types when detecting arrays and dictionaries. It is an Object, and it is impossible to tell whether it is an array or an object. If you want to check whether it is an array or an object, use the instanceof method and constructor method

Second method: instanceof

var obj = {'name':'Tom'}
var arr = ['a','b','c']
var str = 'chenxinming'
var bool = true
var num = 1
var n = null
var fn = function(n){
        console.log(n)
    }
    
console.log(obj instanceof Object)  // true
console.log(arr instanceof Array)    // true
console.log(str instanceof String)    // false
console.log(bool instanceof Boolean)    // false
console.log(num instanceof Number)    // false
console.log(n instanceof Object)    // false
console.log(fn instanceof Function)    // true

Note: instanceof can only be used to determine arrays and objects , function cannot judge string, number and boolean types. If you need to judge string and boolean, you can use the tostring() method.

Special note:

The instanceof method returns True when checking whether the array is an object

var arr = ['a','b','c']

console.log(arr instanceof Object)    // true

If you use instanceof, you need to strictly judge whether it is an array Or when it is an object, the improved code is as follows:

var obj = {'name':'Tom'}
var arr = ['a','b','c']
var str = 'chenxinming'

var getDataType = function(data){
        if(data instanceof Array){
            return 'Array'
            }
        else if (data instanceof Object){
            return 'Object' 
            }
        else{
            return 'data is not obejct type'
        }
    }
    
getDataType(obj)    // Object
getDataType(obj)    // Array
getDataType(obj)    // data is not obejct type

The third method: constructor

var obj = {'name':'Tom'}
var arr = ['a','b','c']
var str = 'chenxinming'
var bool = true
var num = 1
var n = null
var fn = function(n){
        console.log(n)
    }
    
console.log(obj.constructor == Object)  // true
console.log(arr.constructor == Array)  // true
console.log(str.constructor == String)  // true
console.log(bool.constructor == Boolean)  // true
console.log(num.constructor == Number)  // true
console.log(fn.constructor == Function)  // true

The test found that the constructor supports checking Object, Array, String, Boolean, Number, Function

The fourth method: toString (recommended)

var obj = {'name':'Tom'}
var arr = ['a','b','c']
var str = 'chenxinming'
var bool = true
var num = 1
var n = null
var fn = function(n){
        console.log(n)
    }
    
console.log(Object.prototype.toString.call(obj))    // [object Object]
console.log(Object.prototype.toString.call(arr))    // [object Array]
console.log(Object.prototype.toString.call(str))    // [object String]
console.log(Object.prototype.toString.call(bool))    // [object Boolean]
console.log(Object.prototype.toString.call(num))    // [object Number]
console.log(Object.prototype.toString.call(n))    // [object Null]
console.log(Object.prototype.toString.call(fn))    // [object Function]

From the test, it is best to use toString. This method has more comprehensive functions.

The above is the detailed content of How to determine the data type of an object in javascript?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete