MSK Institute LogoMSK.Institute
HomeCourses
Book Free Demo➔Sign In
Free Demo➔
MSK Institute LogoMSK.Institute

Leading computer training academy in Shikohabad for software engineering, Python, web development, and digital literacy.

ISO 9001:2015 Certified Center
Follow & Connect
4.9

Programs & Admissions

  • All CoursesCatalog
  • Learning PathsRoadmaps
  • Student ProjectsShowcase
  • Live Classes & ScheduleLive
  • Upcoming Batches
  • Campuses & LocationsHub
  • Verify Certificates
  • Book Free Demo Class

Resources & Tools

  • Free Online Mock TestsAll Tests
  • NIELIT CCC Mock TestFree
  • Course & Career FinderQuiz
  • Study Material Hub
  • All MSK ToolsHub
  • Keyboard ShortcutsNew
  • TypeQuest Speed LabTyping
  • MSK Code PlaygroundCode
  • Interactive Tutorials
  • Quick Cheatsheets

Institute & Contact

  • About MSK Institute
  • Careers & InternshipsHiring
  • Publications & Blogs
  • Gali No. 3, Near Gyan Jyoti Public School,
    Adarsh Nagar, Shikohabad, Uttar Pradesh 283135
  • +91 83930 42166
  • mskshikohabad@gmail.com
  • Hours:Mon – Sat: 8:00 AM – 7:00 PM | Sun: 10:00 AM – 2:00 PM
  • 4.9 ★(38 Google Reviews)
© 2026 MSK Institute. All rights reserved.•Privacy Policy•Terms of Service•Disclaimer•Contact Us

Shikohabad's Leading Computer & Coding Academy

Home
Courses
Live
Material
Array.from & Array.of0%
Study Materials
JavaScript for IntermediateProgress 0%
1.1Block Scope with let & const1.2Template Literals1.3Destructuring Arrays and Objects1.4Default Parameters1.5Enhanced Object Literals
5.1map, filter, reduce5.2find, some, every5.3Array.from & Array.of5.4Deep Copy vs Shallow Copy
HomeTutorialsJavaScript for IntermediateArray.from & Array.of
Deep Copy vs Shallow Copy

Array.from & Array.of

Beginner12 min read•Updated: 2026-09-15•By Er. Sumit Kumar (MSK Institute)
Study Materials

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.


1. The Pitfall of the Array Constructor

The legacy new Array() constructor behaves inconsistently depending on the number and type of arguments:

1new Array(1, 2, 3); // [1, 2, 3] (Creates 3-element array)
2new Array(5); // [ <5 empty slots> ] (Creates sparse array with length 5, NO elements!)

This unpredictability caused subtle bugs. ES6 introduced Array.of() and Array.from() as standard solutions.


2. Array.of(): Predictable Array Instantiation

Array.of() creates a new Array instance with a variable number of arguments, regardless of number or type:

1Array.of(5); // [5] - Array containing number 5!
2Array.of(1, 2, 3, 4); // [1, 2, 3, 4]
3Array.of(undefined); // [undefined]

3. Array.from(): Converting Iterables and Array-Likes

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).

1 Array-Like or Iterable Array.from(target, mapFn)
2┌─────────────────────────┐ │
3│ NodeList, Set, Map, │ ───────────────────────┼───► Real JavaScript Array
4│ or { length: 5 } │ │ with .map, .filter, etc.
5└─────────────────────────┘

Converting a Set to an Array (Removing Duplicates)

1const tags = ['javascript', 'react', 'javascript', 'css', 'react'];
2const uniqueTags = Array.from(new Set(tags));
3console.log(uniqueTags); // ['javascript', 'react', 'css']

Converting DOM NodeLists

1const buttonNodes = document.querySelectorAll('button');
2const buttons = Array.from(buttonNodes);
3// Now you can safely use .filter() or .slice()

4. Built-in Mapping Function

Array.from() accepts a second optional parameter: a mapFn that executes on every element during conversion. This avoids allocating an intermediate array.

1// Syntax: Array.from(arrayLike, (element, index) => transformedElement)
2 
3const prices = Array.from(
4 document.querySelectorAll('.price-tag'),
5 el => parseFloat(el.textContent.replace('$', ''))
6);

Generating Number Ranges (Range Generator)

1// Create an array containing numbers 1 through 10
2const range1To10 = Array.from({ length: 10 }, (_, index) => index + 1);
3console.log(range1To10); // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
4 
5// Generate alphabet letters A to Z
6const alphabet = Array.from({ length: 26 }, (_, i) => String.fromCharCode(65 + i));
7console.log(alphabet); // ['A', 'B', 'C', ..., 'Z']

Interactive Quiz• Knowledge Check

Practice Quiz

Question 1
What is the result of Array.of(4)?
Question 2
What is the primary difference between Array(3) and Array.of(3)?
Question 3
What is the output of Array.from('CODE')?
Question 4
How can you generate an array of numbers [0, 1, 2, 3, 4] using Array.from()?
Question 5
Why is Array.from(nodeList, mapFn) more efficient than Array.from(nodeList).map(mapFn)?
Next Lesson

Deep Copy vs Shallow Copy

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Related Lessons

Previous LessonNext Lesson
find, some, everyDeep Copy vs Shallow Copy

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

Mentor-Led Practical Training

Master JavaScript for Intermediate with Hands-On Projects

Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.

View Full CourseBook Free Demo
PreviousNext
PreviousPrevNext