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
Creating Custom 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 IntermediateCreating Custom Iterators
Generators with function*

Creating Custom Iterators

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

By implementing the [Symbol.iterator] method on your own custom objects and classes, you can define custom traversal behaviors. This enables custom data structures (such as Linked Lists, Trees, Pagination Streams, and Number Ranges) to work seamlessly with for...of loops, spread syntax, and destructuring.


1. Making a Custom Object Iterable

To make any plain object iterable, attach a method under the computed property key [Symbol.iterator]:

1const countdown = {
2 start: 5,
3 end: 1,
4 
5 [Symbol.iterator]() {
6 let current = this.start;
7 const finalVal = this.end;
8 
9 return {
10 next() {
11 if (current >= finalVal) {
12 return { value: current--, done: false };
13 } else {
14 return { value: undefined, done: true };
15 }
16 }
17 };
18 }
19};
20 
21// Now it works directly with for...of!
22for (const num of countdown) {
23 console.log(num); // 5, 4, 3, 2, 1
24}
25 
26// And spread operator!
27console.log([...countdown]); // [5, 4, 3, 2, 1]

2. Implementing Iterators in Classes: A Range Class

A reusable Range class that can generate numeric sequences:

1class NumberRange {
2 constructor(from, to, step = 1) {
3 this.from = from;
4 this.to = to;
5 this.step = step;
6 }
7 
8 [Symbol.iterator]() {
9 let current = this.from;
10 const stop = this.to;
11 const step = this.step;
12 
13 return {
14 next() {
15 if (current <= stop) {
16 const val = current;
17 current += step;
18 return { value: val, done: false };
19 }
20 return { value: undefined, done: true };
21 }
22 };
23 }
24}
25 
26// Usage:
27const evensUnderTen = new NumberRange(2, 10, 2);
28 
29for (const val of evensUnderTen) {
30 console.log(val); // 2, 4, 6, 8, 10
31}

3. Iterating a Data Structure: Linked List

Iterators decouple data structure internals from consumer loops:

1class ListNode {
2 constructor(value, next = null) {
3 this.value = value;
4 this.next = next;
5 }
6}
7 
8class LinkedList {
9 constructor() {
10 this.head = null;
11 }
12 
13 append(value) {
14 const newNode = new ListNode(value);
15 if (!this.head) {
16 this.head = newNode;
17 return;
18 }
19 let curr = this.head;
20 while (curr.next) curr = curr.next;
21 curr.next = newNode;
22 }
23 
24 [Symbol.iterator]() {
25 let current = this.head;
26 return {
27 next() {
28 if (current) {
29 const val = current.value;
30 current = current.next;
31 return { value: val, done: false };
32 }
33 return { value: undefined, done: true };
34 }
35 };
36 }
37}
38 
39const list = new LinkedList();
40list.append('Alice');
41list.append('Bob');
42list.append('Charlie');
43 
44for (const name of list) {
45 console.log('Customer:', name); // Alice, Bob, Charlie
46}

Interactive Quiz• Knowledge Check

Practice Quiz

Question 1
What must a class implement to enable instance objects to be used with the for...of statement?
Question 2
What happens if an iterator returns { value: 10, done: false } indefinitely without ever setting done: true?
Question 3
What is the return value of [Symbol.iterator]()?
Question 4
Can custom iterators be consumed using the spread operator ([...customIterable])?
Question 5
What should an iterator's next() method return once all elements have been yielded?
Next Lesson

Generators with function*

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

Related Lessons

Previous LessonNext Lesson
Understanding IteratorsGenerators with function*

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