Home >Web Front-end >Front-end Q&A >How to determine whether it is an array in es6

How to determine whether it is an array in es6

WBOY
WBOYOriginal
2022-04-25 18:43:492561browse

In es6, you can use the "Array.isArray()" method to determine whether the object is an array. If the object is an array, the returned result is true. If the object is not an array, the returned result is false. , the syntax is "Array.isArray (js object that needs to be detected)".

How to determine whether it is an array in es6

The operating environment of this tutorial: Windows 10 system, ECMAScript version 6.0, Dell G3 computer.

How to judge whether an array is an array in es6

It is a very common application to judge an array in a program, but in ES5, there will be certain problems if there is no way to strictly judge whether a JS object is an array. Compare The widely recognized method is to use toString to make judgments. Obviously this is not very concise.

ES6 provides the Array.isArray() method to more concisely determine whether a JS object is an array.

Judge the JS object, if the value is Array, it is true; otherwise, it is false.

Syntax usage:

Array.isArray(obj)

obj The JS object that needs to be detected

The example is as follows:

As an example, let’s look at Array.isArray() is How to judge an array.

// 下面的函数调用都返回 true
Array.isArray([]);
Array.isArray([10]);
Array.isArray(new Array());
Array.isArray(new Array('a', 'b', 'c'))
// 鲜为人知的事实:其实 Array.prototype 也是一个数组。
Array.isArray(Array.prototype); 
// 下面的函数调用都返回 false
Array.isArray();
Array.isArray({});
Array.isArray(null);
Array.isArray(undefined);
Array.isArray(17);
Array.isArray('Array');
Array.isArray(true);
Array.isArray(false);
Array.isArray(new Uint8Array(32))
Array.isArray({ __proto__: Array.prototype });

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

Statement:
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