Course offer · HLD · LLD · System Design at ₹799 until 30 Nov (not interview packs)

Iterators Advanced in JavaScript

Issue three tickets in JavaScript with Symbol.iterator and next results containing value and done. Each new loop gets a fresh cursor rather than shared progress.

AdvancedAdvanced JavaScriptExample 7 of 12

Run this example once in a fresh browser console. The console shows the printed results.

iterators-advanced.js
1const tickets = {
2 [Symbol.iterator]() {
3 let number = 1;
4 return {
5 next() {
6 if (number > 3) return { value: undefined, done: true };
7 return { value: 'Ticket ' + number++, done: false };
8 }
9 };
10 }
11};
12console.log([...tickets].join(', '));
13const iterator = tickets[Symbol.iterator]();
14console.log(JSON.stringify(iterator.next()));
15iterator.next(); iterator.next();
16console.log('Finished:', iterator.next().done);
17console.log('Fresh loop:', [...tickets].length);

Sample Output

Ticket 1, Ticket 2, Ticket 3

Spread repeatedly calls next until done is true.

Fresh loop: 3

Each Symbol.iterator call starts a new counter, so the object can supply another full sequence.

In this one

  • Distinguish iterable and iterator
  • Return value and done from next
  • Create independent iteration state
  • Stop at a finite boundary

How it works

  1. 1

    Provide Symbol.iterator on the tickets object.

  2. 2

    Create a fresh number variable inside each iterator call.

  3. 3

    Return Ticket 1 through Ticket 3 with done false.

  4. 4

    Return done true once number exceeds three.

  5. 5

    Compare one manual next result with a fresh spread that still has all three tickets.

Edge cases

Shared cursor

Putting number outside Symbol.iterator would make loops share progress.

Create it inside the method so each loop receives an independent cursor.

Ignoring done

A consumer that never checks done can request values forever.

for...of and spread stop for you. A hand-written loop must inspect done after each next call.

What's going on

An iterator is an object with a next method that provides one value at a time. Each result says both what the value is and whether iteration is finished. This example issues three numbered canteen tickets without storing an array of them first.

An iterable is an object that can supply an iterator through Symbol.iterator. That method is what for...of and array spread look for. Here it creates a new local number each time, so every new loop starts at Ticket 1 rather than sharing the previous loop's progress.

next returns done false while it has a ticket, then done true after the third one. The finished result is a signal to stop, not a fourth ticket. Creating a fresh iterator lets another loop read the three tickets again. This manual form is useful for learning the protocol. A generator often expresses the same sequence with less code because JavaScript creates the next machinery for you.

Quiz

Tap an option. The answer stays hidden until you pick.

Question 1

Which method does array spread look for?

Pick an answer to check it.

Question 2

Which result signals completion?

Pick an answer to check it.

Questions people ask

Is tickets itself an iterator?

Not here. It is iterable because it supplies an iterator, but it has no own next method.

Why does a fresh spread still have three items?

Symbol.iterator creates a new counter on every call.

Is value required after completion?

Consumers stop when done is true. This example uses undefined to make the absence of another ticket clear.