What is Generator next() Method in JavaScript?

Today, we will explore the next() method in JavaScript’s Generator object.

Keep reading to learn how it controls the execution of a generator function and understand its syntax, parameters, and return values.

We will provide practical examples and step-by-step guides on how to use it effectively in your JavaScript code. So what are you waiting for? Let’s dive in!

What is Generator next() Method in JavaScript?

The next() method in JavaScript is a built-in method that is a part of the Generator object.

When you call next(), it resumes the execution of the generator function from where it was last left off, running the code until it hits the next yield statement.

The yield keyword is used in a generator function to pause the function execution and send a value back to the caller.

It’s kind of like a return statement, but instead of ending the function entirely, it just pauses it.

When next() is called with an argument, that argument becomes the value of the entire yield expression where the generator was paused.

Syntax

generatorObject.next(value)

Parameter

value (Optional)

The next() method can take one parameter, value, which is optional. If provided, this value will replace the yield expression in the generator function where execution was last paused.

Return value

The next() method returns an object with two properties:

value

This is the value that was yielded by the yield expression in the generator function.

done

This is a boolean indicating whether or not the generator function has finished executing. If there are no more yield expressions left in the function and it has returned, done will be true. Otherwise, it will be false.

Here’s an illustration:

function* countUp() {
    let count = 0;
    while (true) {
        yield count++;
    }
}

const counter = countUp();

console.log(counter.next().value); ✅
console.log(counter.next().value); 
console.log(counter.next().value); 
console.log(counter.next().value); 
console.log(counter.next().value); 
console.log(counter.next().value); ✅

As what you’ve noticed, each time we call counter.next(), we resume our countUp generator function, which yields the next number in the sequence.

The generator then pauses until we call next() again.

Here’s the output:

0
1
2
3
4
5

In a simple understanding, the next() method is a way to control a generator function’s execution, allowing us to produce a complex sequence of values over time in a way that would be difficult with regular functions.

How to use the Generator next() Method in JavaScript?

Here’s a step-by-step guide on how to use the next() method in JavaScript:

Step 1: Define a Generator Function

First, you need to define a generator function. A generator function in JavaScript is defined like a normal function, but with an asterisk () after the function keyword and uses the yield keyword to yield values.

function* sampleGenerator() {
    yield 'I';
    yield 'Love';
    yield 'Itsourcecode';
}

Step 2: Create a Generator Object

Once you have a generator function defined, you can create a generator object from that function.

let sample = sampleGenerator();

Step 3: Use the next() Method

Now that you have a generator object, you can use the next() method on that object to control the execution of the generator function.

console.log(sample.next().value); 
console.log(sample.next().value); 
console.log(sample.next().value); 

Each time you call next(), it resumes the generator function until it encounters the next yield statement.

The value of the yield statement is returned as part of an object, and execution of the generator function is paused.

When there are no more yield statements left in the function (i.e., the function has returned), calling next() will return { value: undefined, done: true }.

Here’s the complete code:

function* sampleGenerator() {
    yield 'I';
    yield 'Love';
    yield 'Itsourcecode';
}

let sample = sampleGenerator();

console.log(sample.next().value);  
console.log(sample.next().value);  
console.log(sample.next().value);  ✅

Output:

I
Love
Itsourcecode

Here’s another example of how you might use the next() method in a generator function:

function* greet() {
    yield "Hi";
    yield "Welcome";
    yield "to";
    yield "Itsourcecode";


}

const greetings = greet();

console.log(greetings.next().value); ✅
console.log(greetings.next().value); 
console.log(greetings.next().value); 
console.log(greetings.next().value); 
console.log(greetings.next().done);  ✅



In our example code above, we have a generator function greet() that yields four strings: “Hi,” “Welcome,” “to,” and “Itsourcecode.”

We create a generator object greetings from this function.

When we call greetings.next().value, it resumes the generator function and returns the value yielded by the yield expression. The first call returns “Hi” and so on.

After that, when we call greeter.next().done, it returns true because there are no more yield expressions left in the function, indicating that the generator function has finished executing.

Output:

Hi
Welcome
to
Itsourcecode
true

Conclusion

To sump up, the next() method in JavaScript’s Generator object is used to control the execution of a generator function.

It provides a way to produce a sequence of results over time, making it easier to work with asynchronous code.

Understanding and effectively using the next() method can greatly enhance your JavaScript programming skills and open up new possibilities for developing complex applications

We hope this article has provided you with enough information to understand the next javascript.

If you want to explore more JavaScript topics, check out the following articles:

Thank you for reading Itsourcecoders 😊.

Frequently Asked Questions

Is JavaScript still worth learning in 2026?
Yes. JavaScript runs on 98% of websites for the front-end, dominates the back-end via Node.js, powers mobile apps through React Native, builds desktop tools through Electron, and is the scripting layer for most AI tooling (LangChain.js, OpenAI SDK, Vercel AI). Whether you target web, mobile, AI, or full-stack capstones, JavaScript is the broadest single language you can learn.
What is the difference between var, let, and const?
var is function-scoped, hoisted to the top of its scope, and can be redeclared, which leads to bugs in modern code. let is block-scoped (only visible inside the nearest {}) and can be reassigned. const is block-scoped and cannot be reassigned, although object contents can still mutate. Default to const for everything, switch to let only when you actually need to reassign, and avoid var in any code written after 2017.
Which JavaScript version should I target in 2026?
Target ES2020 (ES11) as the safe baseline because every modern browser and Node.js 14+ supports it fully. ES2022 adds useful features like top-level await, private class fields with the # prefix, and the .at() array method. If you are writing for older browsers (IE11 or older Android WebViews), transpile down with Babel or use a build tool like Vite, esbuild, or webpack.
What is the best free editor for JavaScript?
Visual Studio Code is the industry standard, free, with built-in IntelliSense, debugger, terminal, Git, and a huge extension marketplace (ESLint, Prettier, GitHub Copilot, Tailwind). Install the JavaScript and TypeScript Nightly extension for the latest language features. JetBrains WebStorm is more powerful and free for students with a verified .edu email. For quick scratchpad work, the Chrome DevTools Sources panel includes a workspace and breakpoint debugger.
How do I run JavaScript locally vs in the browser?
In the browser: open DevTools with F12 (or right-click then Inspect), go to the Console tab, type or paste your code, press Enter. For HTML pages, add a script tag pointing to your .js file. Locally with Node.js: download Node from nodejs.org (LTS version), then run node script.js in your terminal from the file folder. Use the same Node setup for backend capstones, API integrations, and scripts that do not need a browser.
What can I build with JavaScript for my BSIT capstone?
Common BSIT capstones in JavaScript: full-stack web apps using React or Vue on the front-end with Node.js and Express on the back-end (MongoDB or MySQL for the database), real-time chat or notification systems using Socket.io, single-page dashboards with Chart.js or D3.js, cross-platform mobile apps with React Native, AI-powered chatbots using OpenAI SDK and LangChain.js, and Chrome extensions for productivity tools. Add Tailwind CSS for the UI and Vercel or Netlify for free deployment.
Caren Bautista

Technical Writer at PIES IT Solution

Responsible for crafting clear, well-structured, and beginner-friendly content across the platform. Handles the writing, proofreading, and editorial review of tutorials, guides, and documentation to ensure every article is accurate, readable, and easy to follow.

Expertise: Technical Writing · Content Creation · Documentation · Editorial Writing · JavaScript · TypeScript · Python · Python Errors · HTTP Errors · MS Excel  · View all posts by Caren Bautista →

Leave a Comment