• JavaScript
  • Standard built-in objects
  • Generator.prototype.throw()
  • Inheritance
  • Iterator.prototype.drop()
  • Iterator.prototype.flatMap()
  • Iterator.prototype.some()
  • Object/Function
  • Static methods
    1. Function.prototype.toString()
    2. Function: displayName Non-standard
    3. Function.prototype.arguments Non-standard Deprecated
    4. Function.prototype.caller Non-standard Deprecated
  • Instance methods
    1. Object.prototype.__defineGetter__() Deprecated
    2. Object.prototype.__defineSetter__() Deprecated
    3. Object.prototype.__lookupGetter__() Deprecated
    4. Object.prototype.__lookupSetter__() Deprecated
    5. Object.prototype.toLocaleString()
    6. Object.prototype.__proto__ Deprecated
    7. iterable protocol and the iterator protocol.

      Generator is a subclass of the hidden generator functions:

      js
      function* generator() {
        yield 1;
        yield 2;
        yield 3;
      }
      
      const gen = generator(); / "Generator { }"
      
      console.log(gen.next().value); / 1
      console.log(gen.next().value); / 2
      console.log(gen.next().value); / 3
      

      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.

  • Instance properties

    These properties are defined on Generator.prototype and shared by all Generator instances.

    Generator.prototype.constructor

    The 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.

    Generator.prototype[Symbol.toStringTag]

    The initial value of the Object.prototype.toString().

    Instance methods

    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.

    Examples

    An infinite iterator

    With a generator function, values are not evaluated until they are needed. Therefore a generator allows us to define a potentially infinite data structure.

    js
    function* infinite() {
      let index = 0;
    
      while (true) {
        yield index++;
      }
    }
    
    const generator = infinite(); / "Generator { }"
    
    console.log(generator.next().value); / 0
    console.log(generator.next().value); / 1
    console.log(generator.next().value); / 2
    / …
    

    Specifications

    Specification
    ECMAScript® 2026 Language Specification
    # sec-generator-objects

    Browser compatibility

    See also