De Array.isArray() bepaalt of de gegeven waarde een Array is.
Syntax
Array.isArray(obj)
Parameters
obj- Het te onderzoeken object.
Beschrijving
Indien the object een Array is, dan is true het resultaat, anders wordt dit false.
Bekijk het artikel “Determining with absolute accuracy whether or not a JavaScript object is an array” voor nadere details.
Voorbeelden
// alle van de volgende call resulteren in true
Array.isArray([]);
Array.isArray([1]);
Array.isArray(new Array());
// Weinig bekend: Array.prototype is zelf een array:
Array.isArray(Array.prototype);
// alle van de volgende calls resulteren in 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
De volgende code zal de methode Array.isArray() aanmaken indien deze niet van huis uit werd meegegeven:
if (!Array.isArray) {
Array.isArray = function(arg) {
return Object.prototype.toString.call(arg) === '[object Array]';
};
}
Specificaties
| Specificaties | Status | Commentaar |
|---|---|---|
| ECMAScript 5.1 (ECMA-262) The definition of 'Array.isArray' in that specification. |
Standard | Initiele definitie. Geimplementeerd in JavaScript 1.8.5. |
| ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'Array.isArray' in that specification. |
Standard | |
| ECMAScript 2017 Draft (ECMA-262) The definition of 'Array.isArray' in that specification. |
Draft |
Browser compatibiliteit
| Eigenschap | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
|---|---|---|---|---|---|
| Basic support | 5 | 4.0 (2.0) | 9 | 10.5 | 5 |
| Eigenschap | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
|---|---|---|---|---|---|---|
| Basic support | (Yes) | (Yes) | 4.0 (2.0) | (Yes) | (Yes) | (Yes) |