ES6 introduced static factory methods on the Array constructor—Array.from() and Array.of()—to resolve long-standing quirks with the legacy Array() constructor and simplify conversion of array-like and iterable objects.
The legacy new Array() constructor behaves inconsistently depending on the number and type of arguments:
This unpredictability caused subtle bugs. ES6 introduced Array.of() and Array.from() as standard solutions.
Array.of() creates a new Array instance with a variable number of arguments, regardless of number or type:
Array.from() creates a new, shallow-copied Array instance from an iterable object (Map, Set, String) or an array-like object (an object with a .length property and indexed elements, such as DOM NodeLists or arguments).
Array.from() accepts a second optional parameter: a mapFn that executes on every element during conversion. This avoids allocating an intermediate array.
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
| Previous Lesson | Next Lesson |
|---|---|
| find, some, every | Deep Copy vs Shallow Copy |
Test your understanding of this lesson with 5 questions. Each question has one correct answer.
Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.