yield Keyword
The yield keyword is the heart of generator functions. It pauses the generator’s execution and returns a value to the caller. Each yield expression produces one value in the iterator sequence.
Syntax
Section titled “Syntax”function* generator() { yield expression; // Pauses and returns expression value}How yield Works
Section titled “How yield Works”When the generator encounters a yield:
- The expression is evaluated
- The value is returned as the
valueproperty of the iterator result doneis set tofalse- The generator pauses at that exact point
yield as an Expression
Section titled “yield as an Expression”yield can also receive values from the caller (via next(value)). The value passed to next() becomes the result of the yield expression inside the generator.
Key Points
Section titled “Key Points”- Each
yieldproduces one value in the iteration sequence. - A generator can have zero, one, or multiple
yields. yieldcan be used anywhere an expression is allowed.- Generators automatically have an implicit
return undefinedat the end. yieldin a generator has lower precedence than most operators (use parentheses when needed).
function* threeValues() { yield "first"; yield "second"; yield "third";}
const values = threeValues();console.log(values.next().value); // 'first'console.log(values.next().value); // 'second'console.log(values.next().value); // 'third'
// yield with expressionsfunction* calculationGenerator() { const a = 5; yield a; // Yields 5 yield a * 2; // Yields 10 yield Math.pow(a, 2); // Yields 25}
const calc = calculationGenerator();console.log(calc.next().value); // 5console.log(calc.next().value); // 10console.log(calc.next().value); // 25
// yield receiving values (two-way communication)function* twoWayGenerator() { const received = yield "What is your name?"; yield `Hello, ${received}!`; return "Done";}
const chat = twoWayGenerator();console.log(chat.next().value); // 'What is your name?'console.log(chat.next("Alice").value); // 'Hello, Alice!'console.log(chat.next().value); // 'Done'console.log(chat.next().done); // true
// yield in loopsfunction* timesTable(n) { for (let i = 1; i <= 10; i++) { yield `${n} × ${i} = ${n * i}`; }}
const table = timesTable(7);for (const line of table) { console.log(line);}// 7 × 1 = 7// 7 × 2 = 14// ... up to 7 × 10 = 70