Saurabh Mhatre

Generator functions in JS

A generator function is a popular interview question for web development interviews in Javascript.

Basically, a generator is a function that produces a sequence of results instead of a single value, i.e you generate ​a series of values.

In the below video tutorial we can learn more about it in detail:

Code snippet for reference:-

function* getNames() {
yield "John";
yield "Jim"
}
const genName = getNames();console.log("genName",genName.next().value);console.log("genName 2nd Time", genName.next().value);function* autoIncrementer() {
let counter = 0;
while(true) {
yield counter++;
}
}
const counter = autoIncrementer();
console.log("autoIncrementer", counter.next());

Hope this helps in solving your concept about Generator functions in JS.