Building Arrays with Array.from()
Array.from() builds a new array from an iterable by iterating Symbol.iterator, or from an array-like object by reading its length and indexed properties. It handles strings, Set, Map, generators, NodeList, and {length: n} objects, and throws TypeError for null or undefined.
The short answer
Array.from(items) creates a new array. If items has a callable Symbol.iterator, Array.from iterates it and collects the yielded values. If not, it falls back to treating items as array-like: it reads length, converts it to an integer in the range 0 to 2^53-1, and reads the properties 0, 1, ... up to that length.
That dual behavior is the point. Strings, Set, Map, typed arrays, NodeList, generators, and URLSearchParams are iterable; {0: "a", 1: "b", length: 2} and arguments are array-like. Both become real arrays, and Array.from never returns the input array itself.
A minimal example
Array.from("abc"); // ["a", "b", "c"]
Array.from(new Set([1, 2, 2])); // [1, 2]
Array.from(new Map([["a", 1]])); // [["a", 1]]
Array.from({ length: 3 }, (_, i) => i * 2); // [0, 2, 4]
An array-like without an iterator:
const arrayLike = { 0: "a", 1: "b", length: 2 };
Array.from(arrayLike); // ["a", "b"]
Array.from({ length: 2 }); // [undefined, undefined]
Array.from({ a: 1 }); // [] (no length, so length is 0)
A generator:
function* range(n) {
for (let i = 0; i < n; i++) yield i;
}
Array.from(range(3)); // [0, 1, 2]
Iterable wins over array-like
| Input | Path | Result |
|---|---|---|
[1, 2, 3] |
iterator | shallow copy |
"abc" |
iterator, code points | ["a", "b", "c"] |
new Set([1, 1, 2]) |
iterator | [1, 2] |
{0: "a", length: 1} |
array-like | ["a"] |
{length: 2} |
array-like | [undefined, undefined] |
{a: 1} |
array-like, length 0 | [] |
null or undefined |
neither | TypeError |
If an object has both a length and a Symbol.iterator, the iterator is used and length is ignored. That is why Array.from is not a general "does this look like an array" test; it is a conversion routine. Use Array.isArray() or ArrayBuffer.isView() to test types instead.
Why holes and strings behave the way they do
Array iteration visits every index up to length, so a hole yields undefined rather than being skipped: Array.from([, 1]) returns [undefined, 1], not a sparse array. Strings iterate by Unicode code point, not UTF-16 code unit, so a character outside the Basic Multilingual Plane counts as one element even though string.length counts two.
For array-likes, length passes through ToLength: fractional values are floored, negative values and NaN become 0, and strings such as "3" are converted to 3. The result is a dense array; Array.from fills every index in the range.
Mapping during construction
The second argument is a map function applied as each element is collected, and the third argument sets its this:
const ids = Array.from(
{ length: 3 },
function (_, index) { return `item-${index + 1}`; }
);
// ["item-1", "item-2", "item-3"]
The value over .map() is coverage: this works on iterables and array-likes that have no .map() method. For iterables, index is the loop counter, not necessarily a key in the source.
Edge cases and related conversions
Array.from(params)whereparamsis aURLSearchParamsyields[name, value]pairs, because that object's iterator produces pairs. For the values themselves, see reading repeated parameters with getAll().- Copies are shallow:
Array.from([[1]])shares the inner array with the source. - A plain object with a non-numeric
length, such as{length: 1.9}, produces one element (undefinedat index 0). Array.fromAsync()handles async iterables, but it is newer and less widely available thanArray.from().- Spread (
[...iterable]) also builds an array, but it fails on array-likes that are not iterable, which is exactly the caseArray.fromcovers.
Practical checks
- Use
Array.fromforarguments, DOM collections,Set,Map, and{length}-style objects. - Use
Array.isArray(value)when the question is type, not conversion. - Test with
null,{},{length: 0}, and a generator to confirm which path your code expects.
Sources
Revision history
- Published
- Updated
- — Initial publication.