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
Understanding Iterators0%
Study Materials
JavaScript for IntermediateProgress 0%
1.1Block Scope with let & const1.2Template Literals1.3Destructuring Arrays and Objects1.4Default Parameters1.5Enhanced Object Literals
12.1Understanding Iterators12.2Creating Custom Iterators12.3Generators with function*
HomeTutorialsJavaScript for IntermediateUnderstanding Iterators
Creating Custom Iterators

Understanding Iterators

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

Iteration is the process of looping through elements of a collection one by one. In ES6, JavaScript formalized this process by introducing two core protocols: the Iterable Protocol and the Iterator Protocol. These protocols power fundamental language features such as for...of loops, the spread operator (...), and array destructuring.


1. The Two Iteration Protocols

1. The Iterable Protocol

An object is iterable if it defines an iteration behavior. It does this by implementing a method with the key Symbol.iterator:

  • [Symbol.iterator](): A zero-argument function that returns an object conforming to the Iterator Protocol.

2. The Iterator Protocol

An object is an iterator if it implements a next() method:

  • next(): Must return an object with two properties:
  • value: The current element value (any JS type).
  • done: A boolean (false while iteration continues, true when exhausted).
1 Iterable Object (e.g. Array, Set, Map)
2 │
3 │ Calls [Symbol.iterator]()
4 ▼
5 Iterator
6 │
7 ├──► .next() ──► { value: "A", done: false }
8 ├──► .next() ──► { value: "B", done: false }
9 └──► .next() ──► { value: undefined, done: true }

2. Built-in Iterables

JavaScript includes many built-in iterables:

  • Arrays: [1, 2, 3]
  • Strings: 'hello' (iterates over Unicode code points)
  • Maps: new Map([['a', 1]]) (iterates over [key, value] pairs)
  • Sets: new Set([1, 2, 3])
  • DOM NodeLists: document.querySelectorAll('div')
  • Arguments object: function arguments
1const str = 'Hi!';
2// Acquire the underlying iterator manually:
3const iterator = str[Symbol.iterator]();
4 
5console.log(iterator.next()); // { value: 'H', done: false }
6console.log(iterator.next()); // { value: 'i', done: false }
7console.log(iterator.next()); // { value: '!', done: false }
8console.log(iterator.next()); // { value: undefined, done: true }

3. How for...of Uses Iterators Internally

The for...of loop is syntactic sugar that manages iterators automatically:

1const colors = ['red', 'green', 'blue'];
2 
3// Under the hood, for...of calls colors[Symbol.iterator]()
4// and repeatedly calls iterator.next() until done === true!
5for (const color of colors) {
6 console.log(color);
7}

4. Why Plain Objects are NOT Iterable by Default

Plain objects ({ a: 1, b: 2 }) do not have a [Symbol.iterator] method because the order of object keys was historically ambiguous, and it wasn't clear whether iteration should yield keys, values, or entries.

To iterate over objects with for...of, use helper methods:

1const user = { name: 'Alex', age: 28 };
2 
3// Iterate over keys
4for (const key of Object.keys(user)) { ... }
5 
6// Iterate over values
7for (const val of Object.values(user)) { ... }
8 
9// Iterate over entries (key-value pairs)
10for (const [k, v] of Object.entries(user)) {
11 console.log(`${k}: ${v}`);
12}

Interactive Quiz• Knowledge Check

Practice Quiz

Question 1
What well-known Symbol must an object implement to be recognized as iterable by JavaScript?
Question 2
What two properties must the object returned by an iterator's next() method contain?
Question 3
What does done evaluate to when an iterator has produced its final element and has no more items?
Question 4
Which of the following is NOT an iterable by default in JavaScript?
Question 5
What syntax consumes the Iterable Protocol behind the scenes?
Next Lesson

Creating Custom Iterators

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

Related Lessons

Previous LessonNext Lesson
Practical Use CasesCreating Custom Iterators

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