In JavaScript, every generator function is actually a GeneratorFunction object. There is no global object with the name GeneratorFunction, but you can create a GeneratorFunction() constructor using the following code:

const GeneratorFunction = function* () {}.constructor;


new GeneratorFunction(functionBody)
new GeneratorFunction(arg0, functionBody)
new GeneratorFunction(arg0, arg1, functionBody)
new GeneratorFunction(arg0, arg1, /* … ,*/ argN, functionBody)

GeneratorFunction(arg0, functionBody)
GeneratorFunction(arg0, arg1, functionBody)
GeneratorFunction(arg0, arg1, /* … ,*/ argN, functionBody)

Note: GeneratorFunction() can be called with or without new. Both create a new GeneratorFunction instance.


argN Optional

Names to be used by the function as formal argument names. Each must be a string that corresponds to a valid JavaScript parameter (any of plain identifier, rest parameter, or destructured parameter, optionally with a default), or a list of such strings separated with commas.

As the parameters are parsed in the same way as function declarations, whitespace and comments are accepted. For example: "x", "theValue = 42", "[a, b] /* numbers */" — or "x, theValue = 42, [a, b] /* numbers */". ("x, theValue = 42", "[a, b]" is also correct, though very confusing to read.)


A String containing the JavaScript statements comprising the function definition.


Generator function objects created with a constructor are parsed when the function is created. That is less efficient than declaring a generator function with a function* expression and calling it within your code, because such functions are parsed with the rest of the code.

All arguments passed to the function, except the last, are treated as the names of the identifiers of the parameters in the function to be created, in the order in which they are passed.

Note: generator functions created with a constructor do not create closures to their creation contexts; they are always created in the global scope.

When running them, they will only be able to access their own local variables and global ones, not the ones from the scope in which the GeneratorFunction constructor was called.

This is different from using eval with code for a generator function expression.

Invoking a generator function constructor as a function (without using the new operator) has the same effect as invoking it as a constructor.


Creating and using a GeneratorFunction() constructor

const GeneratorFunction = function* () {}.constructor;
const g = new GeneratorFunction("a", "yield a * 2");
const iterator = g(10);
console.log(; // 20


ECMAScript Language Specification
# sec-generatorfunction-objects

Browser compatibility

BCD tables only load in the browser

See also