offline version v3


119 of 264 menu

Array.from method

The Array.from method returns a new array from an iterable or an array-like object. The first parameter of the method is the object to be used as an array, the second optional parameter is the function to be applied to the elements of the object.

Syntax

let newArray = Array.from(what do we make an array of, [function]);

Example

Let's make a new array from a string:

let res = Array.from('abc'); console.log(res);

The code execution result:

['a', 'b', 'c']

Example

Let's make a new array from a string:

let res = Array.from('abc', function(elem) { return elem + '!'; }); console.log(res);

The code execution result:

  ['a!', 'b!', 'c!']

See also

  • the Array.of method
    that obtains an array from parameters
  • the Array.isArray method
    that checks if an object is an array
enru