JavaScript Array from() Method


The Array.from() method returns an Array object from any object with a length property or an iterable object.

Syntax

Array.from(object, mapFunction, thisValue)

Parameters: This method accepts three parameters as mentioned above and described below:

  • object: This pareter holds that object that will convert into an array
  • mapFunction: This parameter is optional used to call on each item of the array.
  • thisValue: This parameter is optional, it holds the context to be passed as this to be used while executing the mapFunction. If the context is passed, it will be used like this for each invocation of the callback function, otherwise undefined is used as default.

Return value: It returns a new Array instance whose elements are same as the given array. In the case of a string, every alphabet of the string is converted to an element of the new array instance.

Example

var myArr = Array.from("ABCDEFG");

Example -