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
Iterating over Arrays and Objects0%
Study Materials
JavaScript for BeginnersProgress 0%
1.1What is JavaScript?1.2How JavaScript Works in Browser1.3Setting up Environment: VSCode, Live Server
8.1Array Basics and Methods8.2Object Basics and Properties8.3Iterating over Arrays and Objects
HomeTutorialsJavaScript for BeginnersIterating over Arrays and Objects
Working with Date Object

Iterating over Arrays and Objects

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

Iterating (looping) through data collections is one of the most frequent tasks in frontend and backend engineering. Modern JavaScript provides specialized iteration constructs tailored specifically for arrays and objects.


1. Iterating Over Arrays

A. The Modern for...of Loop (Recommended for Values)

Iterates directly over the values of any iterable (Arrays, Strings, Sets):

1const colors = ["Red", "Green", "Blue"];
2 
3for (const color of colors) {
4 console.log(color);
5}
6// "Red", "Green", "Blue"

B. The Array .forEach() Method

Functional iteration taking a callback:

1colors.forEach((color, index) => {
2 console.log(`Index ${index}: ${color}`);
3});

2. Iterating Over Objects: The Holy Trinity of Static Methods

Because objects are not directly iterable with for...of, modern JavaScript provides three static methods on Object:

1const course = { title: "Next.js", price: 499, level: "Advanced" };
2 
3Object.keys(course) --> ["title", "price", "level"] (Array of keys)
4Object.values(course) --> ["Next.js", 499, "Advanced"] (Array of values)
5Object.entries(course) --> [["title", "Next.js"], ["price", 499], ["level", "Advanced"]]

1. Object.keys(): Looping through keys

1for (const key of Object.keys(course)) {
2 console.log(`Key: ${key}, Value: ${course[key]}`);
3}

2. Object.values(): Looping through values directly

1for (const val of Object.values(course)) {
2 console.log(`Value: ${val}`);
3}

3. Object.entries() with Array Destructuring (Most Elegant!):

1for (const [key, value] of Object.entries(course)) {
2 console.log(`${key.toUpperCase()}: ${value}`);
3}
4// TITLE: Next.js
5// PRICE: 499
6// LEVEL: Advanced

3. The for...in Loop (Object Keys & Caution)

The for...in loop iterates over all enumerable property keys of an object:

1const car = { make: "Toyota", model: "Corolla", year: 2024 };
2 
3for (const key in car) {
4 console.log(`${key}: ${car[key]}`);
5}
Important Warning
[!WARNING] Avoid using for...in on Arrays! for...in iterates over property names (as strings, like "0", "1"), does not guarantee numerical order, and can traverse prototype chain properties. For arrays, always use for...of or standard for loops!

4. Summary Matrix: Which Loop Should You Use?

Data StructureBest Modern ChoiceAlternative
Arrayfor...of or .forEach()Standard for (let i = 0; ...)
ObjectObject.entries() with for...ofObject.keys() or for...in
Stringfor...ofStandard for loop

Interactive Quiz• Knowledge Check

Practice Quiz

Question 1
Which loop syntax directly iterates over the elements/values of an array without needing an index counter?
Question 2
What does Object.entries(user) return?
Question 3
Why should you avoid using for...in to iterate through arrays?
Question 4
What does Object.keys({ a: 1, b: 2 }) return?
Question 5
Can you break out of an array.forEach() loop using the break keyword?
Next Lesson

Working with Date Object

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

Related Lessons

Previous LessonNext Lesson
Object Basics and PropertiesWorking with Date Object

Practice Quiz

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

Mentor-Led Practical Training

Master JavaScript for Beginners 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