Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

JS Puzzles · Const

Generator return value

A generator's return(value) ends generator execution and supplies a final value. The caller sees it in the result of next() with done: true, unlike ordinary yielded values.

How it works

A generator can finish with return(value). That value is exposed by the next() result whose done property is true, unlike values produced by yield, which normally have done set to false. Calling generator.return(value) also ends a suspended generator and supplies that value in the result.

Syntax

function* name() { return value; }
generator.return(value)

Parameters

NameTypeDescription
valueanyThe value to expose as the generator's final result when return(value) completes execution.

Returns

The final generator result is an object with value set to the returned value and done set to true.

Key points

  • Generators produce a sequence incrementally; each next() call advances execution until the next yield or completion.
  • Call next() to resume a generator and inspect its iterator result.
  • Each next() result contains value and done, which together describe the yielded or final state.
  • Before completion, value contains the item produced by yield.
  • done distinguishes an active yielded result from the generator's completed result.
  • A generator's explicit return(value) becomes its final iterator value.

Common mistakes

  • Do not read a generator's return value as a yielded item: it appears in the final next() result, marked by done: true.
  • Calling return(value) stops the generator rather than producing another ordinary yielded value.

Example: Reading a generator's final value

function* count() {
  yield 1;
  return "finished";
}

const generator = count();
console.log(generator.next());
console.log(generator.next());

The first call receives the yielded value; the second receives the generator's final return value with done set to true.

When to use it

Returning a final result

A generator performs a final calculation or reports a terminal status after yielding intermediate values.

Explicitly closing a generator

A caller needs to stop a suspended generator early and communicate a value into its completion result.

Inspecting iterator results

Code manually drives a generator and must distinguish yielded items from its terminal value.

Interview questions

QHow is a generator's return value represented?

A generator's return value is delivered by the final next() call as the value property of an object with done: true. Values produced by yield are returned earlier, while the generator is still active.

Follow-up: What happens if next() is called after completion? It returns a completed iterator result, normally with done: true and value: undefined unless another completion behavior applies.

QHow do you explicitly finish a generator with a value?

Use return(value) inside the generator to finish it and provide its final value. The caller observes that value through the next() result after the generator reaches completion.

Follow-up: How can the caller terminate a suspended generator? Call generator.return(value), which finishes execution and returns the supplied value in the iterator result.

QWhat is the difference between yield and return in a generator?

yield pauses execution and sends a value outward, whereas return completes the generator. The next() result uses done: false for an active yielded value and done: true for the completed return value.

Follow-up: Why might a for...of loop not expose the return value? It consumes yielded values and stops when done becomes true, so the final completion value is not emitted as an iteration item.

  • function*
  • yield
  • next()
  • iterable
  • iterator

Source: Generators. Checked 2026-10-15.