Open In App

JavaScript Generator next() Method

Last Updated : 23 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

JavaScript Generator.prototype.next() method is an inbuilt method in JavaScript that is used to return an object with two properties done and value. 

Syntax:

gen.next( value );

Parameters: This function accepts a single parameter as mentioned above and described below:

  • value: This parameter holds the value to be sent to the generator.

Return value: This method returns an object containing two properties:

  • done: It has the value
    • true – for the iterator which is past the end of the iterated sequence.
    • false – for the iterator which is able to produce the next value in the sequence.
  • value: It contains any JavaScript value which is returned by the iterator.

The below examples illustrate the Generator.prototype.next() method in JavaScript

Example 1: In this example, we will create a generator and then apply the Generator.prototype.next() method and see the output.

javascript




function* GFG() {
    yield "GeeksforGeeks";
    yield "JavaScript";
    yield "Generator.prototype.next()";
}
  
const geek = GFG();
console.log(geek.next());
console.log(geek.next());
console.log(geek.next());
console.log(geek.next());


Output:

Object { value: "GeeksforGeeks", done: false }
Object { value: "JavaScript", done: false }
Object { value: "Generator.prototype.next()", done: false }
Object { value: undefined, done: true }

Example 2: In this example, we will create a generator and then apply the Generator.prototype.next() method and see the output.

javascript




function* GFG(len, list) {
    let result = [];
    let val = 0;
  
    while (val < list.length) {
        result = [];
        let i = val
        while (i < val + len) {
            if (list[i]) {
                result.push(list[i]);
            }
            i += 1
        }
  
        yield result;
        val += len;
    }
}
list = [
    'geeks1', 'geeks2', 'geeks3',
    'geeks4', 'geeks5', 'geeks6',
    'geeks7', 'geeks8', 'geeks9',
    'geeks10', 'geeks11'
];
  
let geek = GFG(4, list);
  
console.log(geek.next().value);
console.log(geek.next().value);
console.log(geek.next().value);
console.log(geek.next().value);


Output:

geeks1,geeks2,geeks3,geeks4
geeks5,geeks6,geeks7,geeks8
geeks9,geeks10,geeks11
undefined

Supported Browsers: The browsers supported by Generator.prototype.next() method are listed below:

  • Google Chrome
  • Firefox
  • Opera
  • Safari
  • Edge

We have a complete list of Javascript Generator methods, to check those please go through the Javascript Generator Reference article.



Previous Article
Next Article

Similar Reads

Build a Jokes Generator With Next JS and API
Jokes generator application using Next Js is an application where we are using the external API to fetch the jokes. This application has features to copy the jokes and when the user clicks on "Get a Joke" new jokes will be generated. Preview of final output: Let us have a look at how the final output will look like. Prerequisites:Introduction to Ne
4 min read
Recipe Generator using Next.js
In this tutorial, we'll create a Recipe Generator App using Next.js, a React framework that allows users to generate random recipes. This type of application can be useful for people looking for new meal ideas or wanting to explore different cuisines. application fetches recipe data from API and displays it to the user. Output Preview: Let us have
4 min read
JavaScript Generator throw() Method
JavaScript Generator.prototype.throw() method is an inbuilt method in JavaScript that is used to resume the execution of a generator by throwing an error into it. Syntax: gen.throw(exception); Parameters: This function accepts a single parameter as mentioned above and described below: exception: This parameter holds the exception to be thrown. Retu
2 min read
JavaScript Generator return() Method
JavaScript Generator.prototype.return() method is an inbuilt method in JavaScript that is used to return the given value and finishes the generator. Syntax: gen.return( value ); Parameters: This method accepts a single parameter as mentioned above and described below: value: This parameter holds the value to be returned. Return value: This method r
2 min read
When to use next() and return next() in Node.js ?
In this is article we will see when to use next() and return next() in NodeJS. Features: next() : It will run or execute the code after all the middleware function is finished.return next() : By using return next it will jump out the callback immediately and the code below return next() will be unreachable. Syntax: next() return next() app.use((req
2 min read
How to pass variables to the next middleware using next() in Express.js ?
The following example covers how to pass variables to the next middleware using next() in Express.js. Approach: We cannot directly pass data to the next middleware, but we can send data through the request object. [Middleware 1] [Middleware 2] request.mydata = someData; -------&gt; let dataFromMiddleware1 = request.mydata; Installation of Express m
2 min read
Next.js Create Next App
Next.js is an open-source JavaScript framework created by Vercel which boosts the features of React applications such as Server-Side Rendering and Static Site Generation. It provides many additional features like we get additional data fetching utilities, dynamic API routes, optimized builds, etc. React is used to create Single-Page Applications (S
3 min read
Next.js next/head
In this article, we'll learn about the Head component in NextJS and see how the head component is SEO friendly. The Head component is a built-in component provided by NextJS. We can add any element like we were adding in the head component of HTML. If you know HTML and CSS well then NextJS will be easy for you to learn. To keep everything simple th
4 min read
Next.js next/amp
Next.js is a popular React-based web framework that allows developers to build high-performance, server-rendered React applications easily. It provides many features that ease the development of React applications, including server-side rendering, static site generation, and automatic code splitting. Next.js also provides support for creating Accel
2 min read
Next.ls next.config.js File
Next.js is a framework for building web applications with React that provide additional functionality such as server-side rendering and static site generation. The next.config.js is a configuration file used in Next.js, a popular React framework for building web applications. It allows developers to customize various settings and features of their
3 min read