What keyword is used to define a generator function in JavaScript?
Answer
function*
Answer
function*
The keyword used to define a generator function in JavaScript is `function*`.
The asterisk marks the function as a generator. Calling a generator function does not immediately run its body; instead, it returns a generator object that can be advanced with `.next()`. Execution pauses at each `yield` expression and can later resume with its local state intact.
The asterisk may appear beside `function` or beside the function name, because `function` and `*` are separate tokens: both `function* make() {}` and `function *make() {}` are valid. The `yield` keyword is used inside a generator to produce values, but it does not define the generator itself. `async` defines an asynchronous function; an asynchronous generator uses the combined syntax `async function*`.
Generators became part of ECMAScript 2015, also known as ES6. They remain useful for custom iterators and controlled, lazy sequences, although promises and `async`/`await` are usually preferred for modern asynchronous code.
Source: Wikipedia · fact-checked Aug. 2026