JavaScript (ES2026) reference · Array Methods

JavaScript Array.from() Method

Create a new array from an iterable or array-like object, with an optional function to map each element.

JavaScript (ES2026) Since ES2015

Syntax

Array.from(items, mapFn, thisArg)

Parameters

ParameterDescription
items An iterable (string, Set, Map, …) or array-like object (has length).
mapFn optional Called on each element.

Return value

A new array.

Examples

From strings and setsTry it yourself
console.log(Array.from("hey"));
console.log(Array.from(new Set([1, 1, 2])));

Output

[ 'h', 'e', 'y' ]
[ 1, 2 ]
Build a rangeTry it yourself
console.log(Array.from({ length: 5 }, (_, i) => i * i));

Output

[ 0, 1, 4, 9, 16 ]
From a MapTry it yourself
const map = new Map([["a", 1], ["b", 2]]);
console.log(Array.from(map), Array.from(map.keys()));

Output

[ [ 'a', 1 ], [ 'b', 2 ] ] [ 'a', 'b' ]

Version history

VersionChange
JavaScript ES2015Added

In other languages