JavaScript yield

Beginner
6 min read
Updated: Nov 2026

Definition and Usage

Inside a generator (function*), yield pauses execution and sends a value to the caller. yield* delegates to another iterable or generator.

JavaScript Syntax

JavaScript
function* g() {
  yield "hi";
  yield* [1, 2];
}
console.log([...g()].join(","));
Try it Editor

How It Works

  1. yield. Produces "hi".
  2. yield*. Forwards 1 and 2 from the array.

Examples

Example 1 — Receiving into yield

next(value) sends in.

JavaScript
function* g() {
  const x = yield 1;
  yield x;
}
const it = g();
console.log(it.next().value);
console.log(it.next("z").value);
Try it Editor

How It Works

  1. First next runs to first yield.
  2. Second next passes "z" into yield.

Example 2 — In async generators

Same keyword, async context.

JavaScript
async function* g() { yield 1; }
(async () => {
  for await (const n of g()) console.log(n);
})();
Try it Editor

How It Works

  1. Works with for await...of.

Example 3 — Only in generators

SyntaxError elsewhere.

JavaScript
console.log("yield only inside function* / async function*");
Try it Editor

How It Works

  1. Cannot yield in a normal function.

Frequently Asked Questions

Yes—it forms a yield expression inside generators.
It iterates another iterable and yields each value.

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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.

6 people found this page helpful