Difference Between Array.from and Array.of in JavaScript
Last Updated :
03 Jul, 2024
JavaScript provides various methods for creating and manipulating arrays, two of which are Array.from
and Array.of
. These methods are part of the ECMAScript 6 (ES6) specification and offer distinct ways to create arrays.
Understanding the differences between these two methods is important for efficient array handling in your JavaScript code. This article will explore the differences between Array.from
and Array.of
, their use cases, and examples to illustrate their behaviour.
What is Array.from
Array.from
is a static method that creates a new, shallow-copied array from an array-like or iterable object. This method is particularly useful when you need to convert other types of objects to arrays or when you want to create an array from an iterable with some transformation.
Syntax:
Array.from(arrayLike[, mapFn[, thisArg]])
Parameters:
- arrayLike: An array-like or iterable object to convert to an array.
- mapFn (optional): A mapping function to call on every element of the array.
- thisArg (optional): A value to use as
this
when executing the map function.
Example: Implementation to show example for Array.from
JavaScript
// Example 1: Array from the iterable object
let set = new Set([1, 2, 3]);
let arrayFromSet = Array.from(set);
console.log(arrayFromSet);
// Example 2: Array from string with the mapping function
let str = 'Hello';
let arrayFromString = Array.from(str, x => x.toUpperCase());
console.log(arrayFromString);
Output[ 1, 2, 3 ]
[ 'H', 'E', 'L', 'L', 'O' ]
What is Array.of
Array.of
is a static method that creates a new array instance with a variable number of arguments, regardless of the number or type of the arguments. It is a more straightforward way to create arrays from arguments compared to the Array
constructor.
Syntax:
Array.of(element0[, element1[, ...[, elementN]]])
Parameters:
- elementN: Elements used to create the array.
Example: Implementation to show example for Array.of
JavaScript
// Example 1: Creating an array from the multiple elements
let arr1 = Array.of(1, 2, 3, 4, 5);
console.log(arr1);
// Example 2: Creating an array from the single element
let arr2 = Array.of(10);
console.log(arr2);
Output[ 1, 2, 3, 4, 5 ]
[ 10 ]
Difference Between Array.from and Array.of in JavaScript
Characteristics | Array.from | Array.of |
---|
Purpose | Creates a new array from the array-like or iterable object | Creates a new array with the any number of elements |
---|
Arguments | Takes an array-like or iterable object as the first argument | Takes the any number of arguments as individual array elements |
---|
Use Case | Useful for converting iterable objects into the arrays | Useful for creating arrays with the specific set of the elements |
---|
Flexibility | The Allows mapping and transformation of the array elements | The Accepts any number of the arguments treating each as an element |
---|
Example | Array.from(set) | Array.of(1, 2, 3) |
---|
Similar Reads
Difference between Array and Array of Objects in JavaScript ArrayAn Array is a collection of data and a data structure that is stored in a sequence of memory locations. One can access the elements of an array by calling the index number such as 0, 1, 2, 3, ..., etc. The array can store data types like Integer, Float, String, and Boolean all the primitive dat
3 min read
Difference Between JavaScript Arrays and Objects Below are the main differences between a JavaScript Array and Object.FeatureJavaScript ArraysJavaScript ObjectsIndex TypeNumeric indexes (0, 1, 2, ...)Named keys (strings or symbols)OrderOrdered collectionUnordered collectionUse CaseStoring lists, sequences, ordered dataStoring data with key-value p
1 min read
Difference Between Array.prototype.map() and Array.prototype.flatMap() in JavaScript In JavaScript, Array.prototype.map() and Array.prototype.flatMap() are both the methods used to the transform elements in arrays but they differ in their behaviors regarding handling and flattening of the nested arrays.What is Array.prototype.map()?The map() method creates a new array populated with
3 min read
Difference between forEach and for loop in Javascript In JavaScript, both forEach and for loops are used to iterate over arrays or collections, but they differ in usage and behavior. forEach is a higher-order function that simplifies iteration with built-in array methods, while for loops offer more control, flexibility, and broader application.For Loop
4 min read
What is the difference between Array.slice() and Array.splice() in JavaScript ? In JavaScript, slice() and splice() are array methods with distinct purposes. `slice()` creates a new array containing selected elements from the original, while `splice()` modifies the original array by adding, removing, or replacing elements. slice():The slice() method in JavaScript extracts a sec
3 min read