Suggest an editImprove this articleRefine the answer for “Checking for an array”. Your changes go to moderation before they’re published.Approval requiredContentWhat you’re changing🇺🇸EN🇺🇦UAPreviewTitle (EN)Short answer (EN)**`Array.isArray(value)`** is the standard ES5 method for checking whether a value is an array: it returns `true` if the value is an array and `false` otherwise. **Key point:** it is the only approach that works correctly even across different contexts (iframe, window, and so on).Shown above the full answer for quick recall.Answer (EN)Image## 1. The modern and reliable approach → `Array.isArray()` ```javascript Array.isArray(value); ``` > This is the **standard ES5 method** specifically created for this check. > It returns `true` if the value is an array, and `false` in all other cases. ### Examples for `Array.isArray()` ```javascript Array.isArray([1, 2, 3]); // true Array.isArray([]); // true Array.isArray('text'); // false Array.isArray({ 0: 'a', 1: 'b', length: 2 }); // false Array.isArray(null); // false ``` It works correctly even across different contexts (for example, iframe, window, and so on). This makes it the **only 100% reliable approach**. --- ## 2. The old approach → `instanceof Array` ```javascript value instanceof Array ``` ### Example: ```javascript console.log([1, 2, 3] instanceof Array); // true console.log({} instanceof Array); // false ``` > Drawback: > if the array was created in **a different window or iframe**, the check returns `false`, > because it has a different `Array` constructor. ```javascript // example (pseudocode) iframe.contentWindow.Array !== window.Array; ``` --- ## 3. An alternative (manual) → checking via `Object.prototype.toString` ```javascript Object.prototype.toString.call(value) === '[object Array]' ``` ### Example: ```javascript console.log(Object.prototype.toString.call([1, 2, 3])); // "[object Array]" console.log(Object.prototype.toString.call({})); // "[object Object]" ``` > Works correctly, but looks cumbersome: > nowadays it is almost always replaced with `Array.isArray()`. --- ## Comparing all the approaches | Method | Returns | Pros | Cons | |---|---|---|---| | `Array.isArray()` | `true/false` | Modern, reliable | None | | `instanceof Array` | `true/false` | Clear syntax | Does not work across different contexts | | `Object.prototype.toString.call()` | `"[object Array]"` | Always works | Verbose to write | --- ## Examples of checking different values ```javascript console.log(Array.isArray([1, 2, 3])); // true console.log(Array.isArray('hello')); // false console.log(Array.isArray({ length: 0 })); // false console.log(Array.isArray(new Array())); // true console.log(Array.isArray(null)); // false ``` --- ## In short > Use `Array.isArray(value)`: > it is the **most reliable and readable** way to check whether a value is an array.For the reviewerNote to the moderator (optional)Visible only to the moderator. Helps review go faster.