Difference Between Array.from and Array.of in JavaScript Last Updated : 03 Jul, 2024 Comments Improve Suggest changes Like Article Like Report 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.fromArray.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.ofArray.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 JavaScriptCharacteristicsArray.from Array.ofPurposeCreates a new array from the array-like or iterable object Creates a new array with the any number of elementsArguments Takes an array-like or iterable object as the first argument Takes the any number of arguments as individual array elementsUse Case Useful for converting iterable objects into the arrays Useful for creating arrays with the specific set of the elementsFlexibilityThe Allows mapping and transformation of the array elements The Accepts any number of the arguments treating each as an elementExampleArray.from(set) Array.of(1, 2, 3) Comment More infoAdvertise with us Next Article Difference Between Array.from and Array.of in JavaScript M mguru4c05q Follow Improve Article Tags : Difference Between JavaScript Web Technologies 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 Two Arrays in JavaScript? These are the following ways to Get the Difference Between Two Arrays in JavaScript:1. Using the filter() and includes() Methods - Mostly UsedWe can use the filter() method on the first array and check if each item is not present in the second array using the includes() method. The resulting array c 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 Difference between forEach() and map() loop in JavaScript The forEach() and map() methods in JavaScript are used to iterate over arrays, but they serve different purposes. forEach() executes a provided function once for each array element without returning a new array, while map() transforms elements and returns a new array.JavaScript forEach() JavaScript' 4 min read Differences Between for-in and for-of Statement in JavaScript The for..in loop is designed for iterating over an object's keys or property names, making it useful when accessing each property in an object. Although it can also be used to iterate over the indices of an array. Conversely, the for..of loop is intended to iterate directly over values in iterable c 2 min read Difference Between for...in and Object.keys() in JavaScript The for...in and Object.keys() in JavaScript are used to iterate over the properties of an object. While they might seem similar at first glance they have distinct usage, behavior, and characteristics. This article will explore these differences in detail.These are the following topics that we are g 3 min read How to Filter an Array from all Elements of another Array In JavaScript? Filtering one array from all elements of another array is a common task in JavaScript. This involves removing elements from one array that are present in another array. In this article we will explore different approaches to filter an array from all elements of another array in JavaScript.These are 4 min read Like