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 function* declaration creates a binding of a new generator function to a given name. A generator function can be exited and later re-entered, with its context (variable bindings) saved across re-entrances.
You can also define generator functions using the function* expression.
Note: Generator functions do not have arrow function counterparts.
Note: function and * are separate tokens, so they can be separated by whitespace or line terminators.
The function name.
param OptionalThe name of a formal parameter for the function. For the parameters' syntax, see the Functions reference.
statements OptionalThe statements comprising the body of the function.
A function* declaration creates a GeneratorFunction object. Each time a generator function is called, it returns a new Generator object, which conforms to the iterator protocol. The generator function's execution is suspended at some place, which is initially at the very beginning of the function body. The generator function can be called multiple times to create multiple generators simultaneously; every generator maintains its own execution context of the generator function and can be stepped independently.
The generator allows bidirectional control flow: control flow can transfer between the generator function (callee) and its caller as many times as both parties wish to. Control flow can go from the caller to the callee by calling the generator's methods: next(), throw(), and return(). Control flow can go from the callee to the caller by exiting the function as normal using return or throw or execution all statements, or by using the yield and yield* expressions.
When the generator's next() method is called, the generator function's body is executed until one of the following:
When the generator's throw() method is called, it acts as if a throw statement is inserted in the generator's body at the current suspended position. Similarly, when the generator's return() method is called, it acts as if a return statement is inserted in the generator's body at the current suspended position. Both methods usually finish the generator, unless the generator function catches the completion via try...catch...finally.
Generators used to be a paradigm for asynchronous programming, avoiding Callback Hell by achieving Inversion of Control. Nowadays, this use case is solved with the simpler async functions model and the Promise object. However, generators are still useful for many other tasks, such as defining iterators in a straightforward way.
function* declarations behave similar to function declarations — they are hoisted to the top of their scope and can be called anywhere in their scope, and they can be redeclared only in certain contexts.
| ECMAScript® 2027 Language Specification # sec-generator-function-definitions |
Enable JavaScript to view this browser compatibility table.
This page was last modified on Feb 10, 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.