JavaScript Array from() Method

The Array.from() is a built-in method in JavaScript that creates a new array instance from an array-like or iterable object. It is especially useful for converting objects like NodeList, or the arguments object into an actual array, allowing you to use common array methods.


Array.from(arrayLike[, mapFn[, thisArg]])



It is an array-like or iterable object to convert to an array.

mapFn: Optional

Map function to call on every element of the array.

thisArg: Optional

Value to use as this when executing mapFn.

Return Value

The from() function returns a new  Array instance.

The Array.from(obj, mapFn, thisArg) has the same result as Array.from(obj).map(mapFn, thisArg), except that it does not create the intermediate array.

Example 1

let str = 'appdividned';
let strArr = Array.from(str);



Javascript Array From Example | Array.from() Tutorial

So, it has created an array based on the string. The letters of the string are split into a single element of an array.

Example 2

To create an array from the Set in JavaScript, use Array.from() method , pass the Set as an argument; in return, you will get the Array filled with set values.

let april = new Set(['avengers', 28]); 
let fab = Array.from(april);



Array.from() Tutorial

Example 3

To create an array from the array-like object in Javascript, you can use the Array.from() method , pass the Set as an argument, and in return, you will get the Array.

function arrFrm(){
  return Array.from(arguments);
let arr = arrFrm(1, 2, 3);


OutputJavascript Array From Example

Example 4

To use the arrow function in from() method, pass the first argument as an iterator and the second as an arrow function to process the item of the iterator one by one and return the Array filled with processed elements.

let data = Array.from([1, 2, 3], x => x * x);



Array.from() method example

That is it.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.