GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • JS Tutorial
  • Web Tutorial
  • A to Z Guide
  • Projects
  • OOP
  • DOM
  • Set
  • Map
  • Math
  • Number
  • Boolean
  • Exercise
  • Introduction3 min read
  • Variables5 min read

This view

  • Original page
  • New address

GeezerVison is showing www.geeksforgeeks.org — JavaScript Iterator - GeeksforGeeks

geeksforgeeks
search icon
  • Courses
  • Tutorials
  • Interview Prep
  • JS Tutorial
  • Web Tutorial
  • A to Z Guide
  • Projects
  • OOP
  • DOM
  • Set
  • Map
  • Math
  • Number
  • Boolean
  • Exercise

JavaScript Iterator

Last Updated : 5 May, 2026

A JavaScript iterator is an object that allows sequential access to elements of a collection such as arrays, strings, maps, or sets. It provides a standard way to traverse data structures step-by-step without exposing their internal structure.

  • Iterator Protocol: Iterators follow a protocol that defines how values are produced one at a time.
  • next() Method: Each call to next() returns an object containing value and done.
  • Works with Iterables: Commonly used with iterable objects like arrays, strings, maps, and sets.

Here are different methods of JavaScript Iterator

1. next() Method

The next() method is a core part of the JavaScript Iterator Protocol used to retrieve the next value from an iterator. Each time next() is called, it returns the next element in the sequence until the iteration is complete.

  • Returns an Object: The method returns { value, done }.
  • value: Contains the current element from the collection.
  • done: A boolean indicating whether the iteration has finished.
JavaScript
const numbers = [1, 2, 3];

const iterator = numbers[Symbol.iterator]();

console.log(iterator.next()); // { value: 1, done: false }
console.log(iterator.next()); // { value: 2, done: false }
console.log(iterator.next()); // { value: 3, done: false }
console.log(iterator.next()); // { value: undefined, done: true }
  • The array numbers creates an iterator using Symbol.iterator().
  • Each call to iterator.next() returns the next element of the array along with a done flag indicating whether the iteration is complete.
  • When all elements are accessed, next() returns { value: undefined, done: true }.

2. for...of Loop in JavaScript

The for...of loop is used to iterate over iterable objects such as arrays, strings, maps, and sets. It automatically accesses each element of the collection one by one without needing to manually call next().

  • Works with Iterables: Commonly used with arrays, strings, maps, and sets.
  • Simplifies Iteration: Automatically retrieves values from the iterator.
  • Cleaner Syntax: Easier to read compared to manual iterator usage.
JavaScript
const array = ['a', 'b', 'c'];
const it = array[Symbol.iterator]()
for (let value of it) {
    console.log(value)
}
  • The for...of loop iterates through each element of the array.
  • In each iteration, the current value is assigned to value and printed to the console.
  • The loop automatically stops after all elements are processed.

3. Custom Iterator in JavaScript

A custom iterator allows developers to define their own iteration behavior for an object. It is created by implementing the Symbol.iterator method, which returns an object containing the next() method.

  • Defines Custom Traversal: Controls how values are accessed from an object.
  • Uses Symbol.iterator: This method makes an object iterable.
  • Returns next() Method: next() returns { value, done } during iteration.

Syntax

object[Symbol.iterator] = function () { 
return {
next() {
return { value: ..., done: ... };
}
};
};
JavaScript
const myObject = {
  values: [10, 20, 30],
  [Symbol.iterator]: function () {
    let index = 0;
    return {
      next: () => {
        if (index < this.values.length) {
          return { value: this.values[index++], done: false };
        } else {
          return { done: true };
        }
      }
    };
  }
};

for (const value of myObject) {
  console.log(value);
}
  • The object defines Symbol.iterator, making it iterable.
  • The next() method returns each value from the values array until all elements are accessed.
  • The for...of loop automatically uses this iterator to print 10, 20, and 30.

Advantages of Iterators

  • Provide controlled traversal of collections.
  • Enable compatibility with for...of loops and spread operators.
  • Help implement custom iteration logic for objects.
Comment

Explore

    JavaScript Basics
    • Introduction3 min read
    • Variables5 min read
    • Operators4 min read
    • Control Statements4 min read
    Array & String
    • Arrays6 min read
    • Array Methods4 min read
    • String5 min read
    • String Methods8 min read
    Function & Object
    • Functions4 min read
    • Function Expression3 min read
    • Function Overloading4 min read
    • Objects4 min read
    • Constructors4 min read
    OOP
    • OOP5 min read
    • Classes & Objects4 min read
    • Access Modifiers5 min read
    • Constructor4 min read
    Asynchronous JavaScript
    • Asynchronous2 min read
    • Callbacks4 min read
    • Promises4 min read
    • Event Loop3 min read
    • Async Await Fuction3 min read
    Exception Handling
    • Error and Exceptional Handling5 min read
    • Errors Throw & Try to Catch2 min read
    • Custom Errors2 min read
    • TypeError Invalid Array.prototype.sort argument1 min read
    DOM
    • DOM8 min read
    • DOM Elements3 min read
    • Custom Events4 min read
    • Addeventlistener5 min read
    Advanced Topics
    • Closure3 min read
    • Hoisting5 min read
    • Scope4 min read
    • Higher Order Functions7 min read
    • Debugging3 min read
    Courses
    • Placement 360 Course2 min read
    • MERN Full Stack Course2 min read
    • DSA and System Design Course2 min read
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.