Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

JavaScript · Type Alias

Generator

A Generator is the iterator object produced by a generator function. It combines iterable and iterator behavior, allowing execution to pause at yield and resume later.

How it works

A Generator is the iterator object produced when a generator function is called. It pauses at each yield, resumes through next(), and also supports throw() and return(); because it implements both iterator and iterable protocols, it can be used with for...of and spread syntax.

Syntax

function* name(param0, param1, /* …, */ paramN) {
  statements
}

Returns

A generator function call returns a new Generator object.

Key points

  • Calling a generator function returns an object usable both as an iterator and as an iterable.
  • The built-in Generator type inherits from Iterator.
  • A newly created Generator starts suspended before its function body has executed.
  • The caller controls resumption, exception injection, or completion through next(), throw(), and return().
  • While a yield produces another item, next() reports that item with done set to false.
  • Generators are lazy, evaluating each yielded value only when consumption requests it.

Common mistakes

  • A Generator is single-use: after it is exhausted, subsequent iteration does not replay the yielded values.
  • Do not instantiate Generator with new; obtain one by calling a generator function.
  • Calling a generator function creates the Generator without running its body until next(), iteration, or another control method is used.

Example: Creating and consuming a Generator

function* numbers() {
  yield 1;
  yield 2;
}

const generator = numbers();
console.log(generator.next()); // { value: 1, done: false }
console.log([...generator]); // [2]

A generator function call creates a Generator that yields values on demand and can be consumed with iterator operations or for...of.

When to use it

Lazy sequences

Produce sequence values only as consumers request them, avoiding eager creation of the full sequence.

Infinite or expensive data

Represent an unbounded or computationally expensive sequence without calculating every value in advance.

Custom iteration

Expose custom iteration behavior through for...of, spread syntax, and other iterable consumers.

Cooperative control flow

Pause an algorithm and exchange values bidirectionally between the generator and its caller.

Interview questions

QWhat is a Generator, and how is it obtained?

A Generator is an object returned by calling a generator function. It conforms to both the iterator and iterable protocols, so it provides iterator control methods and can be consumed by iteration constructs.

Follow-up: How does it differ from an ordinary function? A generator function suspends and later resumes execution around yield, while an ordinary function runs to completion when called.

QWhat does Generator.prototype.next() return?

next() resumes the suspended generator and returns an iterator-result object containing value and done. The yielded expression supplies value while done indicates whether iteration has finished.

Follow-up: What does passing an argument to next() do? After the first resume, that argument becomes the result of the currently suspended yield expression.

QHow do return() and throw() affect a Generator?

return() finishes the generator as though a return statement were inserted at its current suspended position. This lets generator cleanup run when the body uses try...finally.

Follow-up: How can an error be injected? throw() resumes the generator by throwing an exception at its suspended position, allowing generator code to catch or propagate it.

  • Generator.prototype.next()
  • Generator.prototype.return()
  • Generator.prototype.throw()
  • GeneratorFunction
  • Iterator
  • function*
  • yield
  • yield*

Source: Generator - JavaScript - MDN. Checked 2026-10-15.