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.

Lesson 1 of 38 steps

Iterable protocol and for...of is a free JavaScript Academy lesson on CoddyKit. This is lesson 1 of 3. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the JavaScript Academy learning path, and your progress syncs across the web and the CoddyKit app. The JavaScript Academy course includes 3 lessons in total.

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
Free to start

Learn JavaScript with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
59
Lessons
200

Frequently Asked Questions

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

Yes — any 3 lessons of the JavaScript Academy learning path, including “Iterable protocol and for...of”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The JavaScript Academy course includes 3 lessons in total.

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, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 3.

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