Home >Web Front-end >Front-end Q&A >How to determine whether it is an array in es6
3 ways to judge: 1. Use the "Array.isArray (array object)" statement to judge. If it is an array, it will return true. 2. Use the "array object.constructor===Array" statement to judge. 3. Use the "array object instanceof Array" statement to judge.
The operating environment of this tutorial: Windows 7 system, ECMAScript version 6, Dell G3 computer.
es6 method to determine whether it is an array:
Method 1: Use the isArray() method
isArray() method is used to determine whether an object is an array.
Returns true if the object is an array, otherwise returns false.
var fruits = ["Banana", "Orange", "Apple", "Mango"]; console.log(Array.isArray(fruits)); if(Array.isArray(fruits)){ console.log("是数组"); }else{ console.log("不是数组"); }
Method 2: Using the constructor attribute
Using the array object.constructor === Array
statement , returns true if it is an array, otherwise returns false.
var fruits = ["Banana", "Orange", "Apple", "Mango"]; console.log(fruits.constructor === Array); if(fruits.constructor === Array){ console.log("是数组"); }else{ console.log("不是数组"); }
Method 3: Use the instanceof operator
The instanceof operator is used to detect whether the prototype attribute of the constructor appears in a certain On the prototype chain of an instance object
var fruits = ["Banana", "Orange", "Apple", "Mango"]; console.log(fruits instanceof Array); if(fruits instanceof Array){ console.log("是数组"); }else{ console.log("不是数组"); }
[Related recommendations: javascript video tutorial, web front-end]
The above is the detailed content of How to determine whether it is an array in es6. For more information, please follow other related articles on the PHP Chinese website!