0Pricing
JavaScript Academy · Lesson

Iterable protocol and for...of

Learn the iterable protocol: Symbol.iterator, iterator.next(), and using for...of, spread, and Array.from with small custom examples.

Iterable protocol and for...of is a free JavaScript Academy lesson on CoddyKit — lesson 1 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the JavaScript Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Big picture

Goal: Read and build tiny iterables.

  • What iterable means
  • Symbol.iterator and iterator.next()
  • for...of, spread, Array.from
  • Build a 0..N counter example
Iterable protocol and for...of — illustration 1

for...of on built-ins

for...of works on built-in iterables like arrays and strings, yielding items in order.

// Arrays and strings are iterable
const arr = [10, 20, 30];
for (const n of arr) {
  console.log("arr item:", n);
}

const word = "JS";
for (const ch of word) {
  console.log("char:", ch);
}
Iterable protocol and for...of — illustration 2

Custom iterable

An iterable has [Symbol.iterator]() that returns an iterator whose next() yields { value, done }.

// A tiny iterable that counts 0..max-1
const Counter = {
  make: function (max) {
    return {
      max: max,
      // Symbol.iterator returns an iterator object
      [Symbol.iterator]: function () {
        let i = 0;
        return {
          next: function () {
            if (i < max) {
              const out = { value: i, done: false };
              i = i + 1;
              return out;
            }
            return { value: undefined, done: true };
          }
        };
      }
    };
  }
};

for (const x of Counter.make(3)) {
  console.log("count:", x);
}
Iterable protocol and for...of — illustration 3

Manual iteration

Under the hood, for...of calls next() repeatedly until done: true.

// You can drive the iterator manually
const iterable = Counter.make(2);
const it = iterable[Symbol.iterator]();

console.log(it.next()); // { value: 0, done: false }
console.log(it.next()); // { value: 1, done: false }
console.log(it.next()); // { value: undefined, done: true }
Iterable protocol and for...of — illustration 4

Spread & Array.from

Spread and Array.from also use the iterable protocol to collect values.

// Spread (...) and Array.from consume iterables
const small = Counter.make(4);
const a1 = [...small];
const a2 = Array.from(Counter.make(3));

console.log("spread:", a1);
console.log("from :", a2);
Iterable protocol and for...of — illustration 5

Good habits

Tips:

  • Return a fresh iterator each time in [Symbol.iterator].
  • Always end with done: true to avoid infinite loops.
  • Keep state local inside the iterator closure.
Iterable protocol and for...of — illustration 6

Iterable protocol quiz

Quick check: Making an object iterable.

Iterable protocol and for...of — illustration 7

Recap

Recap: Built-ins like arrays are iterable. To make your own, add [Symbol.iterator]() that returns an iterator with next(). Use for...of, spread, or Array.from to consume values.

Iterable protocol and for...of — illustration 8

Frequently asked questions

Is the “Iterable protocol and for...of” lesson free?

Yes — the full text of “Iterable protocol and for...of” is free to read here on the web, and the JavaScript Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the JavaScript Academy course, upgrade to CoddyKit PRO.

What will I learn in “Iterable protocol and for...of”?

Learn the iterable protocol: Symbol.iterator, iterator.next(), and using for...of, spread, and Array.from with small custom examples. You practise JavaScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start JavaScript Academy?

No prior experience is required. JavaScript Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 3, so you can start here or from the beginning and move at your own pace.

How long does the “Iterable protocol and for...of” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this JavaScript Academy lesson?

Yes. Every JavaScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Iterable protocol and for...of
  2. Generator functions, yield, and small pipelines
  3. Async iterators — for await...of and async generators
← Back to JavaScript Academy