JS to determine whether a variable is a perfect solution to the array

Source: Internet
Author: User

In the front-end development, in the project, we often need to make an array of variables to determine the type of, of course, even if you have not encountered, but this problem is also to interview when the high-frequency problem, it is necessary to take out to say.

We all know that JS can use TypeOf to determine the basic types of variables, such as:

The code is as follows Copy Code

Alert (typeof ' 111 '); "String"
Alert (typeof 22); "Number"
Alert (typeof a); "Undefined"
Alert (typeof undefined); "Undefined"
Alert (typeof []); "Object"

But this method is not intended to be used to determine the array, because either an array or an object returns object, which requires that we need other methods.

There are three different ways to judge:

1, constructor property

This attribute is added by default when we use the JS system or the object we created ourselves, for example:

var arr = [1,2,3]; Create an Array object
Arr.prototype.constructor = Array; This sentence is the system by default plus

So we can judge this by:

var arr = [1,2,3,1];
Alert (Arr.constructor = = Array); True

2, instanceof

Instanceof is to detect whether the prototype chain of the object points to the prototype object of the constructor, so we can also use it to determine:

var arr = [1,2,3];
Alert (arr instanceof Array); True

3. Use ToString Method

Object.prototype.toString is a method of the top-level object of the prototype chain, which we can use to determine:

var arr = [1,2,3];
Alert (Object.prototype.toString.call (arr) = = ' [Object Array] '); True

4.array.isarray () method

ECMAScript5 will Array.isarray () formally introduced JavaScript, since then we have one more detection

Methods of arrays:

The code is as follows Copy Code

var arr = [1,2,3];
Alert (Array.isarray (arr)); True

Note: said the above four methods, the following notes, the first two methods used when not too insurance, because in the cross-page operation, JS inside the prototype chain is not corresponding, so the first two methods have certain limitations, but generally will not encounter. The fourth method because it is new, the brain residual ie low version of course is not supported, in the standard browser is correct. The best way to do this is to use a third method.

Finally, in order to give you a result, now write a final solution:

The ultimate solution to the judgment array

  code is as follows copy code
var arr = [1,2,3 ];
function Isarrayfn (obj) { //encapsulation of a functions
if (typeof Array.isarray = = "function") {
return Array.isarray (obj) ; Browser support uses the IsArray () method
}else{                     /Otherwise use the ToString method
return Object.prototype.toString.call (obj) = = "[ Object Array] ";
}

Alert (ISARRAYFN (arr));/true

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.