Let arr = [];

1. Check by using the instanceof operator

Start with constructors: You can determine if an object is a property in the stereotype constructor on its stereotype chain.

console.log(arr instanceof Array);   //true
Copy the code

What’s the difference between typeof and Instanceof? Both can be used to determine variables. Typeof returns a primitive type, while Instanceof returns only a Boolean value.

(2)

This property returns the corresponding constructor for the Object. Each instance of Object has a constructor that holds the function used to create the current Object.

console.log(arr.constructor === Array);   //true
Copy the code

3. Use the isArray method provided with the array

The array. isArray method is added in ES5, which is not supported by Internet Explorer 8 and below. It is used to determine whether a value passed is an Array.

console.log(Array.isArray(arr));   //true
Copy the code

4. Use the isPrototypeOf() method

The array. prototype property represents the prototype of the Array constructor.

One such method is isPrototypeOf(), which tests whether an object exists on another object’s prototype chain.

console.log(Array.prototype.isPrototypeOf(arr));   //true
Copy the code

5. Use the Object.getPrototypeOf method

The object.getProtoTypeof () method returns the prototype of the specified Object, so just compare it to the prototype of Array.

console.log(Object.getPrototypeOf(arr) === Array.prototype); // true
Copy the code

6. Through the Object. The prototype. ToString. Judgment call ()

Although Array also inherits from Object, JS overrides toString on Array.prototype, whereas toString.call(arr) is actually called through the prototype chain. You can get different types of variables.

console.log(Object.prototype.toString.call(arr) === '[object Array]');   //true

console.log(Object.prototype.toString.call(arr).slice(8, -1) = = ='Array');   //true

console.log(Object.prototype.toString.call(arr).indexOf('Array')! = = -1);    //true
Copy the code