Get to know MDN better
This feature is well established and works across many devices and browser versions. It’s been available across browsers since September 2016.
The Generator object is returned by a generator function and it conforms to both the iterable protocol and the iterator protocol.
Generator is a subclass of the Iterator class.
There's no JavaScript entity that corresponds to the Generator constructor. Instances of Generator must be returned from generator functions:
There's only a hidden object which is the prototype object shared by all objects created by generator functions. This object is often stylized as Generator.prototype to make it look like a class, but it should be more appropriately called GeneratorFunction.prototype.prototype, because GeneratorFunction is an actual JavaScript entity. To understand the prototype chain of Generator instances, see GeneratorFunction.prototype.prototype.
These properties are defined on Generator.prototype and shared by all Generator instances.
Generator.prototype.constructorThe constructor function that created the instance object. For Generator instances, the initial value is GeneratorFunction.prototype.
Note: Generator objects do not store a reference to the generator function that created them.
The initial value of the [Symbol.toStringTag] property is the string "Generator". This property is used in Object.prototype.toString().
Also inherits instance methods from its parent Iterator.
Generator.prototype.next()Returns a value yielded by the yield expression.
Generator.prototype.return()Acts as if a return statement is inserted in the generator's body at the current suspended position, which finishes the generator and allows the generator to perform any cleanup tasks when combined with a try...finally block.
Generator.prototype.throw()Acts as if a throw statement is inserted in the generator's body at the current suspended position, which informs the generator of an error condition and allows it to handle the error, or perform cleanup and close itself.
With a generator function, values are not evaluated until they are needed. Therefore a generator allows us to define a potentially infinite data structure.
| ECMAScript® 2027 Language Specification # sec-generator-objects |
Enable JavaScript to view this browser compatibility table.
This page was last modified on Jan 24, 2026 by MDN contributors.
Your blueprint for a better internet.
Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation.
Portions of this content are ©1998–2026 by individual mozilla.org contributors. Content available under a Creative Commons license.