Member-only story

Mastering Array.from() and Array.of() in JavaScript: A Comprehensive Guide

Unlock the Power of Array Creation with Ease

Max N
3 min readMar 29, 2024

In the ever-evolving world of JavaScript, developers are constantly seeking efficient and versatile ways to work with arrays. The introduction of Array.from() and Array.of() methods has revolutionized the way we create and manipulate arrays, offering a more concise and expressive approach.

In this article, we'll dive deep into these powerful methods, exploring their functionality, use cases, and practical examples.

Array.from(): Transforming Array-like Objects into Arrays

The Array.from() method is a game-changer when it comes to converting array-like objects or iterable objects into genuine arrays. This method takes an array-like or iterable object as its first argument and returns a new array instance.

// Doubling each element of an array-like object
const arrayLike = { 0: 1, 1: 2, 2: 3, length: 3 };
const doubledArray = Array.from(arrayLike, x => x * 2);
console.log(doubledArray); // Output: [2, 4, 6]

One of the standout features of Array.from() is its ability to accept an optional second argument, a mapping function, which allows you to transform each element of the array-like…

--

--

Max N
Max N

Written by Max N

A writer that writes about JavaScript and Python to beginners. If you find my articles helpful, feel free to follow.

No responses yet