# Iterables & the Iteration Protocol

> Look under the hood of a for...of loop to see how JavaScript uses Symbol.iterator and next() to loop over data.

Subject: JavaScript | Lesson 11 of 63 | 6 min read
Interactive lesson: https://codewithanimation.in/javascript/iteration-protocols
Previous: [Loops: for, while, for...of, for...in](https://codewithanimation.in/javascript/loops.md) | Next: [Functions: Declarations, Expressions & Arrows](https://codewithanimation.in/javascript/functions-basics.md)

## What makes something 'Iterable'?

Why does a `for...of` loop work on Arrays and Strings, but crash if you try to run it on a standard Object?

In JavaScript, an object is only 'iterable' if it implements the Iterable Protocol. This means it must have a special method attached to it using a unique key called `Symbol.iterator`.

## The Iterator Protocol

When a `for...of` loop starts, it looks for the `Symbol.iterator` method on the object and calls it. This method returns an 'Iterator'.

An Iterator is simply an object with a `next()` method. Every time the loop needs a new value, it calls `iterator.next()`.

The `next()` method returns an object with two properties: `value` (the data) and `done` (a boolean). The loop keeps calling `next()` and reading the `value` until `done` is true.

## Manually iterating

You usually let `for...of` handle this automatically, but you can do it manually! First, get the iterator: `const it = array[Symbol.iterator]()`.

Then, call it: `it.next()` might return `{ value: 'apple', done: false }`. Call it again: `it.next()` returns `{ value: undefined, done: true }`.

This protocol is what allows modern JavaScript features like the spread syntax (`...`) and destructuring to work seamlessly across different data types.

## Common mistake: Plain objects aren't iterable

Because plain `{}` objects don't have a `Symbol.iterator` method by default, you cannot use `for...of` on them. To loop over an object's values, you must use `Object.values(obj)` (which returns an array, which IS iterable).

## Tip: Generators make it easy

Writing a custom iterator with `next()`, `value`, and `done` is tedious. JavaScript provides Generator functions (written as `function*`) that automatically create iterators for you using the `yield` keyword.

## Try it: Manual Iteration

Step through an array manually by calling `.next()` on its iterator to see exactly what the `for...of` loop does behind the scenes.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/javascript/iteration-protocols

## Check yourself

### Question 1

What two properties must the object returned by an Iterator's `next()` method contain?

- A. value and done
- B. key and value
- C. current and next

Correct answer: A

The Iterator protocol strictly requires the `next()` method to return an object with a `value` property (the data) and a `done` property (a boolean indicating if the sequence has finished).

- Why not B: Incorrect. Iterators don't intrinsically care about keys, they just return the next value and whether they are done.
- Why not C: Incorrect. The properties are specifically named `value` and `done`.

### Question 2

Which special key is used to define the method that makes an object iterable?

- A. Symbol.iterator
- B. Symbol.iterable
- C. Object.iterator

Correct answer: A

To be iterable, an object must implement a method accessible via `Symbol.iterator`. Built-in iterables like Arrays and Strings have this implemented by default.

- Why not B: Incorrect. While it makes the object iterable, the symbol itself is named `iterator`.
- Why not C: Incorrect. It uses the global Symbol registry.

### Question 3

Why will a `for...of` loop crash if used on a standard plain object?

- A. Plain objects do not implement Symbol.iterator by default.
- B. Object keys are not strings.
- C. You must use `for...in` on all objects, even Arrays.

Correct answer: A

Plain JavaScript objects do not have a built-in `Symbol.iterator` method, meaning they do not comply with the Iterable protocol. Therefore, `for...of` does not know how to extract values from them.

- Why not B: Incorrect. Object keys are usually strings, but that isn't the reason the loop fails.
- Why not C: Incorrect. Arrays are objects and you use `for...of` on them.

## Remember this

- An object is 'iterable' if it has a method at `Symbol.iterator`.
- The iterator method must return an object with a `next()` method.
- `next()` must return an object shaped like `{ value: any, done: boolean }`.
- `for...of`, spread syntax, and destructuring all rely on this exact protocol behind the scenes.
