Array.from()
Creates a new array from an iterable or array-like object, with an optional map function.
Syntax
Array.from(arrayLike, mapFn, thisArg) Parameters
| Name | Type | Required | Description |
|---|---|---|---|
arrayLike | object | Yes | An iterable (e.g. Set, Map, string) or array-like object with a length property. |
mapFn | Function | No | Map function called on every element with (element, index). |
thisArg | any | No | Value to use as `this` when executing mapFn. |
Returns
Array — A new Array instance.
Examples
console.log(Array.from('abc'));
Output
[ 'a', 'b', 'c' ]
console.log(Array.from({ length: 3 }, (_, i) => i * 2));
Output
[ 0, 2, 4 ]
Notes
Works with any iterable (Set, Map, generators) or array-like object. The
mapFn avoids creating an intermediate array compared to `.map()` afterward.
Browser & runtime support
| Environment | Since version |
|---|---|
| chrome | 45 |
| firefox | 32 |
| safari | 9 |
| edge | 12 |
| node | 4.0 |