In JavaScript, whether a variable is an array or an object (array or object) _javascript tips

Source: Internet
Author: User
How can I tell if a JavaScript variable is an array or a obiect?
Answer:
1, if you just use typeof to check the variable, whether it is array or object, will return to ' objec '.
One possible answer to this problem is to check that the variable is not object and check that the variable has a number length (the length may also be 0 when it is an empty array).
However, the Parameter object "arguments object" (passed to all parameters of the Set function) may also apply to the above method, technically, the parameter object is not an array.
Also, when an object has a A.length attribute, this method is not established.
Copy Code code as follows:

Array of real array in progress
var my_array = [];
imposter! An imposter!
var my_object = {};
my_object.length = 0;
Potentially faulty potential errors
function Is_this_an_array (param) {
if (typeof param = = = ' object ' &&!isnan (param.length)) {
Console.log (' Congrats, you have a array! ');
}
else {
Console.log (' Bummer, not a array ');
}
}
Works successful
Is_this_an_array (My_array);
Works, but is incorrect successful, but not correct
Is_this_an_array (My_object);

2. Another answer to this question is to use a more subtle method called the ToString () method to try to convert the variable to a string representing its type.
The method is feasible for real array, and returning [object Arguments] When a parameter object is converted to a string transforms the failure;
The object class that contains the numeric length attribute is also converted to failure.
Copy Code code as follows:

True array of real array
var my_array = [];
imposter! An imposter!
var my_object = {};
my_object.length = 0;
Rock solid Solid (test function)
function Is_this_an_array (param) {
if (Object.prototype.toString.call (param) = = ' [Object Array] ') {
Console.log (' Congrats, you have a array! ');
}
else {
Console.log (' Bummer, not a array ');
}
}
Works worked.
Is_this_an_array (My_array);
Not a array, yay! is not an array!
Is_this_an_array (My_object);

3. In addition, instanceof is a perfect and appropriate operation in a potentially unreliable, multi-framework DOM environment.
Extended reading: "Instanceof considered harmful ..."
Http://perfectionkills.com/instanceof-considered-harmful-or-how-to-write-a-robust-isarray
Copy Code code as follows:

var my_array = [];
if (My_array instanceof array) {
Console.log (' Congrats, you have a array! ');
}

4, for JavaScript 1.8.5 (ECMAScript 5), variable name. IsArray () can do this
Https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/isArray
Copy Code code as follows:

var my_array = [];
if (Array.isarray (My_array)) {
Console.log (' Congrats, you have a 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.