Generators in JavaScript - Everything You Should Know | Top JavaScript Developers
loader image
Javascript FrameWorks

Generators In JavaScript With Examples

Introduction to generators

Generators in Javascript are one of the special class functions that simplify your task of writing iterators. Javascript Generators generate a series of values. Generators can be defined as the normal functions, but they can return (yield) multiple values whereas normal functions can have only one return value.

Yield statement in the generator function will pause the function execution when it is called and it does have the functionality to resume, which will continue its execution from the last yield run.

From the above example, it looks like a normal function definition, but since it is a generator function, it has function* in it, instead of the function keyword. And it uses yield statement instead of the return statement, by which it can yield multiple values from a single function.

ALSO READ: MongoDB In Golang With Examples – A Beginner’s Guide

Generator Object

Generator functions will return objects when it is called. It can be used by looping the object because it is iterable or we can use the next method in these objects.

Output:

When the done becomes true, it means that the generator function is in its last execution, it will no more return values.

So we can get the value by using the generator object,

When the last yield statement returns it’s valued, if it is called again, then it will return the value undefined.

Once the generated value is exhausted, we cannot iterate it again. All we can do is to create a new generator object & then use it.

Iterating Generator

Generators are also iterables, hence we can simplify the code for the implementation of the iterator. And we need not to manually store the next() method and the current state.

Output:

ALSO READ: How To Develop A Healthcare Mobile Application For Hospitals

Infinite Data Generator Functions

It is possible to create generator functions that will never end. In the below function, the while loop will always become true, and it will yield value whenever the function is called.

Generators Observers

Generators will act as an observer since it will act when it receives a value. It will receive value from the next() function. Hence it keeps observations for value when it receives it will act immediately.

Do you find it interesting? you might also like these articles. Top 10 Best Tech Companies For Employees To Work In The USA In 2020 and Top 10 IT Staffing and Recruiting Agencies in the USA.

If you have a business idea in your mind and in search for a reliable web development company, you are in the right place. Hire the best web developers in the industry from Agira technologies.

Looking for a Tech partner to dominate the digital world?
The following two tabs change content below.
Logakrishnan

Logakrishnan

A Full-Stack web developer, expertise in various Javascript frameworks including Node JS and React JS. He is fond of exploring ethical hacking and penetration testing. Logakrishnan is a person who loves to share his knowledge of technologies and is also an active instructor at Udemy.
Logakrishnan

Latest posts by Logakrishnan (see all)

Leave a Reply

Your email address will not be published. Required fields are marked *

Join our 30,000+ subscribers, never miss out anything on our latest blogs, tips, tutorials, updates & more.