Skip to the solution
HardFrontend4 min read

How do iterators and the iterable protocol work?

433 views

The problem

Question presented to candidate: "What's actually the difference between an 'iterator' and an 'iterable' in JavaScript β€” aren't they the same thing? And if I hand you an object with just a next() method, will for...of work on it directly?"

What a strong answer should cover:

  • πŸ“Œ Interview term: the iterator protocol β€” an object is an iterator if it has a next() method that returns a real { value, done } object each call β€” verified directly, hand-driving one through 4 calls, including the final { value: undefined, done: true }.
  • πŸ“Œ Interview term: the iterable protocol β€” an object is an iterable if it has a [Symbol.iterator]() method that RETURNS an iterator β€” a genuinely separate, distinct protocol from being an iterator itself.
  • πŸ“Œ Interview term: the real, direct answer to the prompt β€” verified directly: a raw object with only a next() method (an iterator, but not an iterable) genuinely fails with a real TypeError when used directly with for...of β€” for...of, spread syntax, and destructuring all specifically look for [Symbol.iterator], not next() alone.
  • πŸ“Œ Interview term: built-in iterables β€” verified directly: arrays and strings genuinely have a real, built-in [Symbol.iterator], while a plain object genuinely does not β€” this is exactly why for...of works natively on arrays/strings/Maps/Sets but throws on a plain object (covered in more depth in this bank's own for...of vs. for...in question).
  • A precise answer names that many objects are BOTH iterators and iterables at once β€” a generator object (covered in this bank's own dedicated generator-function question) genuinely has both a working next() AND its own [Symbol.iterator] that just returns itself, which is exactly why a generator can be driven manually with .next() calls AND used directly with for...of.

Clarifying questions expected:

  • None β€” this is a definitional/technical question; directly answering whether a bare iterator alone works with for...of (it does not) is the strong signal.

Code / implementation expected: Yes β€” a real, manually-implemented iterator AND a separate real iterable wrapping it, verified working differently with for...of, is the clearest, most convincing demonstration.

  • iterators

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

4 min read Β· 9 sections

Run the code

Edit the code and run it here. Each console line with an expected value becomes a test.

JavaScriptReal proof: a bare iterator fails with for...of, but wrapping it as an iterable via Symbol.iterator fixes it

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 151 of 165 in JavaScript

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

Up nextHard

What is the difference between microtasks and macrotasks?

Continue