JavaScript Generator next() Method

Beginner
7 min read
Updated: Nov 2026
3 examples
Instance method

Definition

A generator is a function that can pause. next() is the button that makes it run. Each call starts or resumes the generator and lets it run until the next yield, where it pauses again.

The method always returns a small object with two properties: value (what was yielded) and done (false while there is more to come, true when the generator has finished). You can also give next() a value, and the paused yield receives it.

Remember
const result = generator.next(valueToSend);

result.value  → what the generator yielded (or returned)
result.done   → false = paused, more to come
                true  = finished

valueToSend becomes the answer of the paused yield.
The very first next() ignores it.

Syntax

Create a generator object by calling a function* function, then call next() on that object.

JavaScript
function* myGenerator() {
  yield 1;
}

const generator = myGenerator();

generator.next();
generator.next(value);
CallWhat it does
gen.next()Run until the next yield and return { value, done: false }.
gen.next("Ava")Same, and the paused yield becomes "Ava" inside the generator.
gen.next().valueA short way to get just the yielded value.

Built into JavaScript (ES2015). Every generator object has it; no import required. Siblings: return() and throw().

Library declarations
JavaScript
// Generator.prototype.next (ES2015)
generator.next()
generator.next(value)

// returns { value: any, done: boolean }

Parameters

ParameterDescription
valueOptional. A value that is sent into the generator. It becomes the result of the yield expression where the generator is paused. The first call to next() has no paused yield yet, so the value is ignored.

Return Value

Return ValueDescription
{ value, done: false }The generator paused at a yield. value is what was yielded.
{ value, done: true }The generator finished. value is what the generator returned, or undefined when it returned nothing.
TypeError (thrown)next() was called while the generator was already running, for example from inside the generator itself.

JavaScript Examples

Example 1 — Run a Generator One Step at a Time

The generator prints a line before each yield. Watch when each line appears.

JavaScript
function* steps() {
  console.log("inside: start");
  yield "first";
  console.log("inside: middle");
  yield "second";
  console.log("inside: end");
}

function show(result) {
  return "value: " + result.value + ", done: " + result.done;
}

const gen = steps();
console.log("Generator created, nothing ran yet");

console.log("1) " + show(gen.next()));
console.log("2) " + show(gen.next()));
console.log("3) " + show(gen.next()));
console.log("4) " + show(gen.next()));
Try It Yourself

How It Works

  1. Creating is not running. steps() only builds the generator object, so the first line printed is the one outside the generator.
  2. Each next() runs to a yield. The first next() prints "inside: start" and pauses at yield "first". The text "inside: ..." appears before the matching result line, because the generator runs while next() is being evaluated.
  3. The third call finishes it. The generator prints "inside: end" and runs off the end, so done is true and value is undefined. The fourth call is harmless and gives the same result.

Example 2 — Send a Value into a Generator

The value you give to next() becomes the answer of the yield that was waiting.

JavaScript
function* chat() {
  const name = yield "What is your name?";
  const hobby = yield "Hello " + name + "! What do you like?";
  return name + " likes " + hobby;
}

const gen = chat();

console.log(gen.next("ignored").value);
console.log(gen.next("Ava").value);

const last = gen.next("chess");
console.log(last.value);
console.log(last.done);
Try It Yourself

How It Works

  1. The first next() ignores its value. No yield is waiting yet, so "ignored" goes nowhere. The generator runs to the first yield and gives back the question.
  2. The second next() answers it. gen.next("Ava") makes the waiting yield produce "Ava", so name is "Ava". The generator then asks the second question.
  3. The last next() finishes. hobby becomes "chess" and the return sends back the final sentence. Because the generator ended, done is true.

Example 3 — Pull Values from an Endless Generator

This generator never finishes, but it does not need to, because it only works when asked.

JavaScript
function* ids() {
  let id = 1;

  while (true) {
    yield "user-" + id;
    id++;
  }
}

const gen = ids();

console.log(gen.next().value);
console.log(gen.next().value);
console.log(gen.next().value);

const firstFour = [];

for (const id of ids()) {
  firstFour.push(id);
  if (firstFour.length === 4) {
    break;
  }
}

console.log(firstFour.join(", "));
Try It Yourself

How It Works

  1. A loop that does not hang. while (true) would freeze a normal function. In a generator it pauses at every yield, so the loop only moves one step per next().
  2. Ask for what you need. Three calls to gen.next().value give user-1, user-2 and user-3. The generator remembers id between calls.
  3. for...of calls next() for you. The loop stops itself with break after four values. It uses a fresh ids() generator, so it starts again at user-1.

Browser & Runtime Support

Generators arrived with ES2015. Every current browser and Node.js version supports them, including next().

✓ ES2015 · Widely supported

generator.next()

Supported in Chrome 39+, Edge 13+, Firefox 26+, Safari 10+ and current Node.js, Deno and Bun. Internet Explorer never got generators.

ES2015 Standard since
Google Chrome 39+ · Desktop & Mobile
Full support
Microsoft Edge 13+ · Legacy & Chromium
Full support
Mozilla Firefox 26+ · Desktop & Mobile
Full support
Apple Safari 10+ · macOS & iOS
Full support
Opera 26+ · Chromium based
Full support
Samsung Internet Recent versions · Android
Supported
Android WebView Recent versions · Chromium based
Supported
Node.js All current versions · Server runtime
Supported
Deno All versions · JavaScript runtime
Supported
Bun All versions · JavaScript runtime
Supported
Internet Explorer No native support
Not supported
generator.next() Excellent

Bottom line: Safe to use in every current browser and in Node.js. Only Internet Explorer lacks function*, and it is no longer supported.

Frequently Asked Questions

It starts or resumes the generator and runs it until the next yield (or until it finishes). It returns an object with two properties: value, the yielded value, and done, which is false while the generator can give more values and true when it is finished.
A value passed to next() becomes the result of the yield where the generator is paused. Before the first next() there is no paused yield yet, so the value has nowhere to go and is ignored. Pass values from the second call on.
Nothing bad. It keeps returning { value: undefined, done: true } and the generator code does not run again. That makes it safe to keep calling next() in a loop until done is true.
No. for...of, the spread operator [...gen] and Array.from(gen) call next() for you until done is true. Call next() by hand when you want full control, for example to send values in or to pull only a few values.
yield pauses the generator and gives a value out with done: false, and the generator can continue later. return finishes the generator and gives a final value with done: true. Loops such as for...of ignore the returned value and only use the yielded ones.

Next: return()

Learn how to stop a generator early, and how its finally block still gets a chance to clean up.

return() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

15 people found this page helpful