How to determine whether it is an array in es6

青灯夜游
Release: 2022-05-05 17:20:03
Original
5379 people have browsed it

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.

How to determine whether it is an array in es6

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("不是数组");
}
Copy after login

How to determine whether it is an array in es6

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("不是数组");
}
Copy after login

How to determine whether it is an array in es6

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("不是数组");
}
Copy after login

How to determine whether it is an array in es6

[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!

Related labels:
es6
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!