typeof all Return Object
All data types in JavaScript are strictly objects, but in practice we still have types, if we want to determine whether a variable is an array or an object using TypeOf, because it all returns object
123456 |
var o = {
‘name‘
:
‘lee‘ };
var a = [
‘reg‘
,
‘blue‘
];
document.write(
‘ o typeof is ‘ +
typeof o);
document.write(
‘ <br />‘
);
document.write(
‘ a typeof is ‘ +
typeof a);
|
Perform:
O typeof is Object
A typeof is Object
Therefore, we can only abandon this method, to determine whether an array or object has two methods
First, use the TypeOf plus the length property
The array has the length property, object does not, and the TypeOf array and object all return object, so we can judge
1234567891011121314151617181920 |
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:
12345 |
var o = { ‘name‘ : ‘lee‘ }; var a = [ ‘reg‘ , ‘blue‘ ]; alert( a instanceof Array ); // true alert( o instanceof Array ); // false |
You can also tell if it belongs to an object
12345 |
var o = { ' name ' : ' Lee ' }; var a = [ ' reg ' ' Blue ' ]; alert (a instanceof object); //true Alert (o instanceof object); //true |
But the array is also object, so the above two are true, so we have to use instanceof to determine whether the data type is an object or an array should be the first to judge the array, and finally to determine the object
123456789101112131415161718 |
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 prioritize arrays, such as:
123456789101112131415161718 |
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 is also judged to be object.
Definition and usage
The length property can set or return the number of elements in the array.
Grammar
Arrayobject.length
Description
The length property of an array is always 1 larger than the subscript of the last element defined in the array. For regular arrays that have continuous elements and begin with element 0, the attribute length declares the number of elements in the array.
The length property of an array is initialized when an array is created with the constructor array (). When you add a new element to an array, the value of length is updated if necessary.
Set the Length property to change the size of the array. If you set a value that is smaller than its current value, the array is truncated and the elements at its tail are lost. If the value is set larger than its current value, the array will grow, and the new elements are added to the end of the array, their values are undefined.
JavaScript determines whether a variable is an object or an array