JavaScript is used to determine whether a variable is an object or an array. It is a javascript array.
All typeof objects are returned.
In JavaScript, all data types are strictly objects, but we still have different types in actual use. If you want to determine whether a variable is an array or an object uses typeof, because it returns all objects
Copy codeThe Code is as follows:
Var o = {'name': 'lil '};
Var a = ['reg ', 'Blue'];
Document. write ('o typeof is '+ typeof o );
Document. write ('<br/> ');
Document. write ('a typeof is '+ typeof );
Run:
Copy codeThe Code is as follows:
O typeof is object
A typeof is object
Therefore, we can only discard this method. There are two methods to determine whether an array or object is used.
First, use the typeof plus length attribute
The array has the length attribute, the object does not exist, and both the typeof array and the object return the object, so we can judge this way.
Copy codeThe Code is as follows:
Var o = {'name': 'lil '};
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
You can use instanceof to determine whether a variable is an array, for example:
Copy codeThe Code is as follows:
Var o = {'name': 'lil '};
Var a = ['reg ', 'Blue'];
Alert (a instanceof Array); // true
Alert (o instanceof Array); // false
You can also determine whether it belongs to an object.
Copy codeThe Code is as follows:
Var o = {'name': 'lil '};
Var a = ['reg ', 'Blue'];
Alert (a instanceof Object); // true
Alert (o instanceof Object); // true
However, the array also belongs to the object, so both of the above are true. Therefore, we should use instanceof to determine whether the data type is an object or an array first, and finally determine the object.
Copy codeThe Code is as follows:
Var o = {'name': 'lil '};
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 do not prioritize Array, for example:
Copy codeThe Code is as follows:
Var o = {'name': 'lil '};
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 is determined as an object.
How does Javascript determine whether a variable is a common variable, an array, or an object?
1. Use the typeof operator to detect variable types
Array, Null, Object Type
String type
True and false are boolean types.
Integer and floating point type: number Type
2. If you want to distinguish between arrays and non-array objects, you need to use constructors to determine
If (arr. constructor = Array)
// Arr is an array
Else
// Arr is not an array
How can I determine whether the variables in javascript are arrays?
Var array = [];
Alert (Object. prototype. toString. call (array) = '[object Array]')