Array(3) creates three holes, so Array.of(3) exists to create [3]. Array.from() copies any iterable (string, Set, NodeList) or array-like object into a real array, mapping as it goes. Array.fromAsync() (ES2026) does the same for async iterables and returns a Promise.
| Method | Returns |
|---|---|
| Array.isArray(value) | true for arrays, even from other frames |
| Array.of(...items) | New array of the arguments |
| Array.from(source, mapFn?) | New array, optionally mapped |
| Array.fromAsync(source, mapFn?) | Promise for a new array |
console.log(Array(3), Array.of(3), Array.from('abc'));
console.log(Array.from({ length: 4 }, (_, i) => i * i)); // array-like + mapFn
const wait = (ms, v) => new Promise((ok) => setTimeout(ok, ms, v));
async function* pages() { yield ['a', 'b']; yield wait(50, ['c']); }
console.log(Array.from([wait(10, 'x'), 'y'])); // Promises left as-is
console.log(await Array.fromAsync([wait(10, 'x'), 'y'])); // each element awaited
console.log(await Array.fromAsync(pages(), (p) => p.length));[ <3 empty items> ] [ 3 ] [ 'a', 'b', 'c' ]
[ 0, 1, 4, 9 ]
[ Promise { <pending> }, 'y' ]
[ 'x', 'y' ]
[ 2, 1 ]Array.fromAsync() awaits one value at a time, like for await...of (Asynchronous Iteration): ideal for draining a paginated API, but slower than Promise.all() for promises already running. It has been in all major browsers since January
Prefer Array.isArray() to instanceof Array, which fails for arrays from another iframe.