JavaScript Generator return() Method

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

Definition

return() ends a generator early. You call it when you are happy with the values you have and do not want the rest. The generator stops at the point where it is paused and is marked as finished.

It returns { value, done: true }, where value is the argument you passed. If the generator was paused inside a try block, its finally code still runs, which makes return() the right way to let a generator clean up.

Remember
const result = generator.return(finalValue);

result.value  → finalValue (or undefined)
result.done   → always true

The generator is finished: next() now gives
{ value: undefined, done: true }.
finally blocks inside the generator still run.

Syntax

Call return() on a generator object. The argument is optional.

JavaScript
generator.return();
generator.return(finalValue);
CallWhat it does
gen.return()Finish the generator. Returns { value: undefined, done: true }.
gen.return("stop")Finish the generator and use "stop" as the final value.
for (const x of gen) { break; }Leaving a for...of loop early with break calls return() for you.

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

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

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

Parameters

ParameterDescription
valueOptional. The final value of the generator. It is returned as result.value. If you leave it out, value is undefined.

Return Value

Return ValueDescription
{ value, done: true }The generator is finished. value is the argument you passed.
A yield inside finallyIf a finally block yields, the generator pauses there and the result has done: false. This is rare.
TypeError (thrown)return() was called while the generator was running.

JavaScript Examples

Example 1 — Stop a Generator Early

Take one value, then call return() and see that nothing more comes out.

JavaScript
function* numbers() {
  yield 1;
  yield 2;
  yield 3;
}

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

const gen = numbers();

console.log(show(gen.next()));
console.log(show(gen.return("stop")));
console.log(show(gen.next()));
Try It Yourself

How It Works

  1. Take one value. The first next() gives 1 and the generator waits at yield 1.
  2. Finish early. gen.return("stop") ends the generator right there. The result has done: true and the value you passed, "stop".
  3. Nothing more. The values 2 and 3 are never produced. The next next() says done: true with no value.

Example 2 — Let finally Close a Resource

A generator opens something and closes it in finally. See that return() triggers the cleanup.

JavaScript
function* openFile() {
  console.log("file opened");

  try {
    yield "line 1";
    yield "line 2";
  } finally {
    console.log("file closed");
  }
}

const gen = openFile();

console.log(gen.next().value);
console.log(gen.return("early exit").value);
console.log(gen.next().done);
Try It Yourself

How It Works

  1. Open and read. The first next() prints "file opened", enters the try block and pauses at yield "line 1".
  2. return() jumps to finally. Instead of continuing, gen.return("early exit") makes the generator leave the try block. The finally code runs and prints "file closed" before the result line.
  3. Finished for good. The returned value is "early exit", and a later next() reports done: true.

Example 3 — See break and Not-Started Generators

A break in a for...of loop calls return() for you. A generator that never started behaves differently.

JavaScript
function* tidy() {
  try {
    yield "a";
    yield "b";
    yield "c";
  } finally {
    console.log("cleanup ran");
  }
}

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

for (const letter of tidy()) {
  console.log(letter);

  if (letter === "b") {
    break;
  }
}

const fresh = tidy();

console.log(show(fresh.return("never started")));
console.log(show(fresh.next()));
Try It Yourself

How It Works

  1. break calls return(). When the loop stops at "b", JavaScript calls return() on the generator for you. That is why "cleanup ran" is printed even though you never wrote return().
  2. A fresh generator. fresh was created but never started with next(), so the code inside it never ran, and neither did its try or finally.
  3. It is closed anyway. fresh.return("never started") gives done: true with your value, and next() afterwards has nothing left to give. No "cleanup ran" is printed a second time.

Browser & Runtime Support

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

✓ ES2015 · Widely supported

generator.return()

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.return() 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 ends the generator early. The generator stops where it is paused, its finally blocks run, and the call returns { value, done: true } with the value you passed. After that, next() only returns { value: undefined, done: true }.
They have a similar effect but come from different places. The return statement is written inside the generator and ends it from the inside. The return() method is called from outside, on the generator object, and ends the generator from the outside.
Yes. When you leave a for...of loop early with break (or with return or an error), JavaScript calls return() on the generator for you. That is how finally blocks in generators get their chance to clean up.
Yes, if the generator is paused inside a try block that has a finally. The finally code runs first and the result comes afterwards. If the generator has not started yet, or is already finished, there is nothing to clean up and nothing runs.
Always { value: undefined, done: true }. A generator cannot be restarted. To get the values again, call the generator function once more to create a new generator object.

Next: throw()

Learn how to send an error into a paused generator, and how the generator can catch it.

throw() 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.

12 people found this page helpful