A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/javascript/whats-the-yield-keyword-in-javascript/ below:

What is the yield Keyword in JavaScript?

What is the yield Keyword in JavaScript?

Last Updated : 12 Jul, 2025

The yield keyword in JavaScript is used to pause and resume a generator function asynchronously. A generator function works similarly to a normal function, but instead of returning values with return, it uses yield.

This allows for the function's execution to be paused and resumed at specific points. The yield expression returns an object containing two properties: value, which holds the yielded value, and done, a boolean that indicates whether the generator function has been completed (true) or is still in progress (false).

Pausing a yield pauses the generator, and it resumes when the next() method is called. The function continues executing until it encounters another yield or return statement.

Example 1: This example shows the usage of yield keyword in JavaScript.

javascript
function* showPrices(i) {
    while (i < 3) {
        yield i++;
    }
}

// Creating an object for our function showPrices
const gfg = showPrices(0); 

// Return 0 because 0 value is passed to the 
// showPrices yield expression
console.log(gfg.next().value); 

// return 1
console.log(gfg.next().value); 

//return 2
console.log(gfg.next().value); 

Example 2: This example shows the usage of yield keyword in JavaScript.

javascript
function* geeksforGeeks() {

  	// Expression paused here and return value 
  	// is undefined as nothing is declared yield; 
  
	// Wait for next() to finish
    gfg(yield "Welcome to GFG"); 
}

function gfg(x) {
    console.log("Hello World ", x)
}

let generator = geeksforGeeks();

//return undefined
console.log(generator.next()); 

//return Welcome to GFG 
console.log(generator.next()); 

Output
{ value: 'Welcome to GFG', done: false }
Hello World  undefined
{ value: undefined, done: true }


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4