1. isArray()
isArray()
method is used to determine whether an object is an array. Return if the object is an array true
, otherwise return false
.
Array.isArray(arr); // true
2. Object prototype
Use the prototype chain to determine whether it has the top of the same prototype chain as the array.
arr.__proto__ === Array.prototype; // true
Three, instanceof operator
Used to detect whether the properties of the constructor prototype
appear on the prototype chain of an instance object
arr instanceof Array; // true
四、Object.prototype.toString.call()
All primitive data types can be judged by this method, which is universal
Object.prototype.toString.call(arr); // "[object Array]"
Object.prototype.toString.call(2); // "[object Number]"
Object.prototype.toString.call(""); // "[object String]"
Object.prototype.toString.call(true); // "[object Boolean]"
Object.prototype.toString.call(undefined); // "[object Undefined]"
Object.prototype.toString.call(null); // "[object Null]"
Object.prototype.toString.call(Math); // "[object Math]"
Object.prototype.toString.call({}); // "[object Object]"
Object.prototype.toString.call([]); // "[object Array]"
Object.prototype.toString.call(function () {}); // "[object Function]"
Reprint: Several methods to determine whether a variable is an array - short book