JavaScript Array.from() Method
The Array.from() static method creates a new array from an iterable or array-like object, such as a string, Set or NodeList, optionally mapping each element.
Description
The Array.from() static method creates a new array from any iterable or array-like object. It converts things like a NodeList from querySelectorAll(), the characters of a string, the values of a Set or the entries of a Map into a real array with all the array methods.
The optional mapping function runs on each element as it is added. Combined with an object that only has a length, Array.from({ length: 5 }, (_, i) => i) is a neat way to generate a sequence of numbers.
Array.from() also accepts the result of map.entries(), set.values() and generator functions, and works on typed arrays to create regular arrays. Unlike spread syntax, which only works with iterables, it also handles array-like objects that aren't iterable, such as { length: 2, 0: 'a', 1: 'b' }. Array.fromAsync() does the same for async iterables, returning a promise.
Syntax
Array.from(arrayLike)
Array.from(arrayLike, mapFn)
Array.from(arrayLike, mapFn, thisArg)Parameters
- arrayLikeAn iterable (such as a string, Set, Map or NodeList) or an object with a
lengthproperty. - mapFnOptional. A function to call on every element, like
map(). - thisArgOptional. A value to use as
thisinsidemapFn.
Return Value
A new array.
Example
Browser Support
The following information will show you the current browser support for the Array.from() method. Hover over a browser icon to see the version that first introduced support for this method.
Desktop





Tablets & Mobile





