ForgeFrontend — Prepare, Practice, Crack
Secure checkout
Lifetime access
Instant Drive delivery
Free updates forever

Prepare · Practice · Crack

What are generators and iterators?

Quick answer

An iterator exposes next() returning {value, done}. A generator (function* + yield) is the easy way to build one and produce values lazily.

In detail

The iterator protocol — an object with a `[Symbol.iterator]` returning `{ next() }` — is what makes `for...of` and spread work. Generators pause at each `yield`, producing values on demand, which is great for lazy/infinite sequences and was the foundation async/await was built on.

function* ids() { let i = 1; while (true) yield i++; }
const g = ids();
g.next().value; // 1
g.next().value; // 2

Why interviewers ask this: A deeper question at product companies.

This is 1 of 118+ questions in the JavaScript Interview Kit

Get every question with detailed answers, follow-ups and real code — plus coding challenges and a last-minute revision sheet. One-time payment, instant access.

⚡ Get the JavaScript Interview Kit → ₹299

Full kit

JavaScript Interview Kit · ₹299

Get it →