Favorites

JavaScript function* Statement

☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee
function*
JavaScript Functions and ClassesThis statement is supported by all modern browsers.
Updated 28th November 2026

The function* declaration defines a generator function, which can pause with yield and produce a sequence of values one at a time.

Description

The function* declaration defines a generator function. Calling it doesn't run the body; it returns a Generator object. Each call to the generator's next() runs the body until the next yield, which produces a value and pauses. When the function returns, the generator is done.

Generators make it easy to create iterators, including infinite sequences, because values are only computed when they're asked for. They work directly with for...of, spread syntax and iterator helpers.

A generator can also receive values: the argument passed to next(value) becomes the result of the paused yield expression, which allows two-way communication. yield* delegates to another generator or iterable, yielding each of its values in turn. Generator functions can't be arrow functions, and they can't be used with new.

Syntax

function* name(params) {
  yield value;
}

Example

function* idMaker(prefix) {
  let id = 1;
  while (true) yield prefix + id++;
}
const ids = idMaker('user-');
console.log(ids.next().value, ids.next().value);

function* take3(list) { yield* list.slice(0, 3); }
console.log([...take3(['a', 'b', 'c', 'd'])]);

Browser Support

The following information will show you the current browser support for the function* statement. Hover over a browser icon to see the version that first introduced support for this statement.

This statement is supported by all modern browsers.

Desktop

Chrome
Edge
Firefox
Opera
Safari

Tablets & Mobile

Chrome Android
Firefox Android
Opera Android
Safari iOS
Samsung Internet
Android WebView
-
Supported Unknown Not supported
☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee