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 JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav 11 min read Web Development Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De 5 min read React Interview Questions and Answers React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications 15+ min read JavaScript Interview Questions and Answers JavaScript (JS) is the most popular lightweight, scripting, and interpreted programming language. JavaScript is well-known as a scripting language for web pages, mobile apps, web servers, and many other platforms. Both front-end and back-end developers need to have a strong command of JavaScript, as 15+ min read React Tutorial React is a JavaScript Library known for front-end development (or user interface). It is popular due to its component-based architecture, Single Page Applications (SPAs), and Virtual DOM for building web applications that are fast, efficient, and scalable.Applications are built using reusable compon 8 min read Difference Between IPv4 and IPv6 In the digital world, where billions of devices connect and communicate, Internet Protocol (IP) Addresses play a crucial role. These addresses are what allow devices to identify and locate each other on a network.To know all about IP Addresses - refer to What is an IP Address?Currently, there are tw 9 min read Domain Name System (DNS) DNS is a hierarchical and distributed naming system that translates domain names into IP addresses. When you type a domain name like www.geeksforgeeks.org into your browser, DNS ensures that the request reaches the correct server by resolving the domain to its corresponding IP address.Without DNS, w 8 min read Difference between BFS and DFS Breadth-First Search (BFS) and Depth-First Search (DFS) are two fundamental algorithms used for traversing or searching graphs and trees. This article covers the basic difference between Breadth-First Search and Depth-First Search.Difference between BFS and DFSParametersBFSDFSStands forBFS stands fo 2 min read Differences between TCP and UDP Transmission Control Protocol (TCP) and User Datagram Protocol (UDP) both are protocols of the Transport Layer Protocols. TCP is a connection-oriented protocol whereas UDP is a part of the Internet Protocol suite, referred to as the UDP/IP suite. Unlike TCP, it is an unreliable and connectionless pr 9 min read NodeJS Interview Questions and Answers NodeJS is one of the most popular runtime environments, known for its efficiency, scalability, and ability to handle asynchronous operations. It is built on Chromeâs V8 JavaScript engine for executing JavaScript code outside of a browser. It is extensively used by top companies such as LinkedIn, Net 15+ min read Like