Javascript Array.from()

The from() method creates a new array from any array-like or iterable object.

Example


from() Syntax

The syntax of the from() method is:

Array.from(arraylike, mapFunc, thisArg)

The from() method, being a static method, is called using the Array class name.


from() Parameters

The from() method can take three parameters:

  • arraylike - Array-like or iterable object to convert to an array.
  • mapFunc (optional) - Map function that is called on each element.
  • thisArg (optional) - Value to use as this when executing mapFunc.

Note: Array.from(obj, mapFunc, thisArg) is equivalent to Array.from(obj).map(mapFunc, thisArg).


from() Return Value

  • Returns a new Array instance.

Note: This method can create an array from:

  • array-like objects - The objects that have length property and have indexed elements like String.
  • Iterable objects like Map or Set.

Example 1: from() Method with Array-like Objects

Output

['J', 'a', 'v', 'a', 'S', 'c', 'r', 'i', 'p', 't']

In the above example, we have used the from() method to create a new array from the 'JavaScript' string.

We have called the method using Array class as Array.from("JavaScript"). The method returns a new array ['J', 'a', 'v', 'a', 'S', 'c', 'r', 'i', 'p', 't'] and assigns it to array1.


Example 2: from() Method with Mapping Function

Output

[ 4, 6, 8 ]

In the above example, we have passed a mapping function in the from() method that increases every element of the array being created by 2.


Example 2: from() Method with a Set

Output

[ 'JavaScript', 'Python', 'Go' ]

Here, we have created a new array using the from() method with an iteratable object- Set.


Recommended Reading: JavaScript Array map()