JavaScript Generators & Iterators Interview Questions and Answers

Explain the iterable and iterator protocols, generator functions, yield, and lazy sequences.

Frequently Asked Questions

Important questions are highlighted in the sections below; select one to jump to its answer.

Theoretical JavaScript Interview Questions

Generators are functions that can pause mid-execution with yield and resume later when you ask for the next value. Defined with function*, they return an iterator you call .next() on. This is useful for lazy sequences (generating values only on demand) and implementing custom iteration. In day-to-day code you'll see them less than async/await, but understanding them helps with advanced patterns.

An iterator is an object that lets you go through a sequence of values one at a time. It has a next() method that returns an object with two properties: value (the current item) and done (a boolean that is true when there are no more items). Arrays, strings, Maps, and Sets are all iterable in JavaScript.

javascript

const arr = [10, 20, 30];
const it = arr[Symbol.iterator]();

console.log(it.next()); // { value: 10, done: false }
console.log(it.next()); // { value: 20, done: false }
console.log(it.next()); // { value: 30, done: false }
console.log(it.next()); // { value: undefined, done: true }

A generator function is a special kind of function that can pause in the middle of running and be resumed later. You define one using function*. Calling a generator function does not execute it immediately. It returns a generator object instead. You then call .next() on that object to run the function up to the next yield statement, pause it there, and get back the yielded value.

javascript

function* greet() {
  yield "Hello";
  yield "World";
}

const gen = greet();
console.log(gen.next()); // { value: "Hello", done: false }
console.log(gen.next()); // { value: "World", done: false }
console.log(gen.next()); // { value: undefined, done: true }

yield pauses the generator and hands a value back to the caller. It's essentially a temporary return that can happen multiple times. The next time you call .next(), the function picks up from exactly where it stopped. You can also send a value back INTO the generator by passing it to .next(value), and that value becomes what the yield expression evaluates to inside the function.

javascript

function* add() {
  const x = yield "Give me a number";
  const y = yield "Give me another";
  return x + y;
}

const gen = add();
gen.next();    // { value: "Give me a number", done: false }
gen.next(5);   // { value: "Give me another", done: false } - 5 becomes x
gen.next(3);   // { value: 8, done: true }                 - 3 becomes y

A regular function runs from start to finish in one shot and returns one value. A generator function can pause in the middle using yield, return multiple values over time, and resume from where it stopped. A regular function always starts fresh when you call it. A generator object keeps its state between .next() calls.

Each time you call .next(), the generator runs until it hits the next yield statement, then pauses. It returns an object with two keys: value (the yielded value) and done (a boolean: false while the generator still has more to yield, true once it has finished). If you pass a value into .next(value), that value becomes the result of the paused yield expression inside the function.

javascript

function* count() {
  yield 1;
  yield 2;
  yield 3;
}

const gen = count();
console.log(gen.next()); // { value: 1, done: false }
console.log(gen.next()); // { value: 2, done: false }
console.log(gen.next()); // { value: 3, done: false }
console.log(gen.next()); // { value: undefined, done: true }

yield* delegates to another iterable or generator. Instead of yielding a single value, it yields all values from another generator (or array, string, etc.) one by one. It is useful for composing generators together.

javascript

function* inner() {
  yield 2;
  yield 3;
}

function* outer() {
  yield 1;
  yield* inner(); // delegate to inner
  yield 4;
}

console.log([...outer()]); // [1, 2, 3, 4]

Lazy evaluation means you only compute a value when you actually need it. Generators are lazy by nature because they do not run until you call .next(). This is useful for infinite sequences or large datasets where you do not want to compute everything upfront.

javascript

// Infinite sequence - only generates values when asked
function* naturals() {
  let n = 1;
  while (true) {
    yield n++;
  }
}

const gen = naturals();
console.log(gen.next().value); // 1
console.log(gen.next().value); // 2
console.log(gen.next().value); // 3
// This never crashes even though it's "infinite"

An iterator has a next() method returning objects with a value and a done Boolean. An iterable provides [Symbol.iterator]() that returns an iterator.

yield produces a value and suspends the generator, preserving its local state until next() resumes it.

It repeatedly calls the generator iterator protocol and assigns each yielded value until the iterator reports done: true.

The generator completes and next() reports done: true with that value. Standard for...of iteration does not include that final return value.

yield* delegates iteration to another iterable or generator, forwarding its yielded values and, for generators, receiving its completion value.

Calling a generator function creates a suspended execution context; each next() resumes at the previous yield with local bindings retained.

It requests completion and normally causes a suspended generator finally block to run before it reports done. Put cleanup code in finally where appropriate.