Home > Web Front-end > JS Tutorial > body text

JavaScript method to determine whether a variable is an object or an array_javascript skills

WBOY
Release: 2016-05-16 16:38:27
Original
1254 people have browsed it

typeof all returns object

All data types in JavaScript are objects in the strict sense, but in actual use we still have types. If you want to determine whether a variable is an array or an object, you can’t use typeof because it all returns object

Copy code The code is as follows:

var o = { 'name':'lee' };
var a = ['reg','blue'];

document.write( ' o typeof is ' typeof o);
document.write( '
');
document.write( ' a typeof is ' typeof a);

Execution:
Copy code The code is as follows:

o typeof is object
a typeof is object

Therefore, we can only give up this method. There are two ways to determine whether it is an array or an object

First, use typeof plus length attribute

Array has length attribute, object does not, and typeof array and object both return object, so we can judge this way

Copy code The code is as follows:

var o = { 'name':'lee' };
var a = ['reg','blue'];

var getDataType = function(o){
If(typeof o == 'object'){
If( typeof o.length == 'number' ){
               return 'Array';
         }else{
                  return 'Object';                                              }
}else{
           return 'param is no object type';
}
};

alert( getDataType(o) ); // Object
alert( getDataType(a) ); // Array
alert( getDataType(1) ); // param is no object type
alert( getDataType(true) ); // param is no object type
alert( getDataType('a') ); // param is no object type

Second, use instanceof

Use instanceof to determine whether a variable is an array, such as:


Copy code The code is as follows:
var o = { 'name':'lee' };
var a = ['reg','blue'];

alert( a instanceof Array ); // true
alert( o instanceof Array ); // false

You can also determine whether it belongs to object

Copy code The code is as follows:
var o = { 'name':'lee' };
var a = ['reg','blue'];

alert( a instanceof Object ); // true
alert( o instanceof Object ); // true

But arrays also belong to objects, so both of the above are true. Therefore, when we use instanceof to determine whether the data type is an object or an array, we should first determine array, and finally determine object

Copy code The code is as follows:
var o = { 'name':'lee' };
var a = ['reg','blue'];

var getDataType = function(o){
If(o instanceof Array){
         return 'Array'
}else if( o instanceof Object ){
         return 'Object';
}else{
           return 'param is no object type';
}
};

alert( getDataType(o) ); // Object
alert( getDataType(a) ); // Array
alert( getDataType(1) ); // param is no object type
alert( getDataType(true) ); // param is no object type
alert( getDataType('a') ); // param is no object type


If you don’t judge Array first, for example:
Copy code The code is as follows:

var o = { 'name':'lee' };
var a = ['reg','blue'];

var getDataType = function(o){
If(o instanceof Object){
         return 'Object'
}else if( o instanceof Array ){
         return 'Array';
}else{
           return 'param is no object type';
}
};

alert( getDataType(o) ); // Object
alert( getDataType(a) ); // Object
alert( getDataType(1) ); // param is no object type
alert( getDataType(true) ); // param is no object type
alert( getDataType('a') ); // param is no object type

Then the array will also be judged as object.
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!