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.

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

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