An iterable implements [Symbol.iterator]() (strings, Set, Map, generators, arguments). An array-like is any object with a length and indexed properties ({ length: 2, 0: 'a', 1: 'b' }). Only iterables work with spread and for...of; [...{ length: 2 }] throws a TypeError.
Array.from(items, mapper?, thisArg?):
- Throws if
mapperis given and not callable. - If
itemshas aSymbol.iteratormethod, steps the iterator and collects the values. - Otherwise treats
itemsas an array-like: readslength, thenitems[0..length-1]. Missing indices becomeundefined(no holes). - The mapper receives
(value, index)only and runs as the values are produced, so there is no intermediate array.
items of null or undefined throw a TypeError; numbers and booleans give []. If this is a constructor, it is used to build the result (so subclasses work). Array.of(...items) is the sibling that builds from arguments: Array.of(7) is [7], while Array(7) is seven holes.
Array.from({ length: n }, fn) is the idiomatic way to build n real elements. new Array(n) creates holes, which map and forEach skip.
Array.fromAsync(items, mapper?, thisArg?) returns a Promise of an array. It checks Symbol.asyncIterator first, then Symbol.iterator, then falls back to an array-like. It processes one item at a time: it requests the next item, awaits it, and only then asks for the following one. The mapper's result is awaited, so an async mapper works. For sync iterables and array-likes each element is awaited too. It does not run work in parallel: for that, start the promises first and use Promise.all.
Availability: Node 24.19 has it. MDN marks it Baseline "Widely available" (since January 2024), and the proposal is Stage 4. Check your target runtimes for older environments.