Our volunteers haven't translated this article into 正體中文 (繁體) yet. Join us and help get the job done!
Summary
The Array.isArray() method returns true if an object is an array, false if it is not.
Syntax
Array.isArray(obj)
Parameters
-
obj - The object to be checked.
Description
See the article “Determining with absolute accuracy whether or not a JavaScript object is an array” for more details.
Examples
// all following calls return true
Array.isArray([]);
Array.isArray([1]);
Array.isArray(new Array());
// Little known fact: Array.prototype itself is an array:
Array.isArray(Array.prototype);
// all following calls return 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({ __proto__: Array.prototype });
Polyfill
Running the following code before any other code will create Array.isArray() if it's not natively available.
if (!Array.isArray) {
Array.isArray = function(arg) {
return Object.prototype.toString.call(arg) === '[object Array]';
};
}
Specifications
| Specification | Status | Comment |
|---|---|---|
| ECMAScript 5.1 (ECMA-262) The definition of 'Array.isArray' in that specification. |
Standard | Initial definition. Implemented in JavaScript 1.8.5. |
| ECMAScript 6 (ECMA-262) The definition of 'Array.isArray' in that specification. |
Draft |
Browser compatibility
| Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
|---|---|---|---|---|---|
| Basic support | 5 | 4.0 (2.0) | 9 | 10.5 | 5 |
| Feature | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
|---|---|---|---|---|---|---|
| Basic support | (Yes) | (Yes) | 4.0 (2.0) | (Yes) | (Yes) | (Yes) |
Based on Kangax's compat table.
See also
Document Tags and Contributors
標籤:
Contributors to this page: Mingun, julienw, gelliot, cesar, rgrove, evilpie, Lcf.vs, Nesk, Crash, Daniel Hug, Sheppy, chernikov, fscholz, ziyunfei, Nickolay, trevorh, MrDanielLewis, fusionchess, wesj, SphinxKnight, dbruant, ethertank
最近更新:
SphinxKnight,