creation
Array.isArray
Returns true if the value is an Array (works across realms/iframes, unlike `instanceof Array`). Reach for it as the canonical type check before running array-only methods.
Array.isArray(value) Parameters
| Parameter | Purpose |
|---|---|
| value | Any value to test |
| return value | boolean — true only for real Array instances (including from other realms) |
Examples
console.log(Array.isArray([1, 2, 3])); Logs: true
console.log(Array.isArray('abc'), Array.isArray({length: 1})); Logs: false false
console.log(Array.isArray(new Int8Array(3))); Logs: false (TypedArrays are NOT arrays)
console.log(Array.isArray(Array.from('ab'))); Logs: true
Gotcha
TypedArrays (Int8Array, Uint8Array, etc.) are NOT arrays and return false. Use this instead of `instanceof Array`, which returns false for arrays coming from another iframe/realm.
Related methods
Array.from
Creates a new Array from an iterable or array-like object, optionally mapping each element as it is copied (ES2015). Reach for it to materialize a NodeList, Set, string, or `{length}` array-like into a real array.
Array.prototype.includes
Returns true if the array contains searchElement using SameValueZero equality (ES2016). Reach for it as the readable boolean alternative to `indexOf(x) !== -1`.
Array.prototype.some
Returns true if the callback returns truthy for at least one element, short-circuiting on the first hit. Reach for it to answer "does anything match?" without walking the full array.