JavaScript determines whether a variable is an object or an array

Source: Internet
Author: User

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) );    // Objectalert( getDataType(a) );    // Arrayalert( getDataType(1) );    // param is no object typealert( getDataType(true) ); // param is no object typealert( getDataType(‘a‘) );  // param is no object type
Second, use instanceof

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

12345 varo = { ‘name‘:‘lee‘ };var a = [‘reg‘,‘blue‘]; alert( a instanceof Array );  // truealert( o instanceofArray );  // 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) );    // Objectalert( getDataType(a) );    // Arrayalert( getDataType(1) );    // param is no object typealert( getDataType(true) ); // param is no object typealert( 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) );    // Objectalert( getDataType(a) );    // Objectalert( getDataType(1) );    // param is no object typealert( getDataType(true) ); // param is no object typealert( 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.