JavaScript Array.from()
Example
Create an array from a string:
let text = "ABCDEFG";
Array.from(text);
Try it Yourself »
Array.from(text);
More examples below !
Description
The Array.from()
method returns an array from any object with a length property.
The Array.from()
method returns an array from any iterable object.
Note
Array.from() is a static property of the JavaScript Array object.
You can only use it as Array.from().
Using x.from(), where x is an array will return undefined.
Syntax
Array.from(object, mapFunction, thisValue)
Parameters
Parameter
Description
object
Required.
The object to convert to an array.
The object to convert to an array.
mapFunction
Optional.
A map function to call on each item.
A map function to call on each item.
thisValue
Optional.
A value to use as
A value to use as
this
for the mapFunction
Return Value
Type
Description
An array The values from the iterable object.
More Examples
Example
Create an array from an array:
const myNumbers = [1,2,3,4];
const myArr = Array.from(myNumbers, (x) => x * 2);
Try it Yourself »
const myArr = Array.from(myNumbers, (x) => x * 2);
Array Tutorials:
Browser Support
Array.from()
is an ECMAScript6 (ES6 2015) feature.
JavaScript 2015 is supported in all browsers since June 2017:
Chrome 51 |
Edge 15 |
Firefox 54 |
Safari 10 |
Opera 38 |
May 2016 | Apr 2017 | Jun 2017 | Sep 2016 | Jun 2016 |