An Introduction to Iterators and Generators in JavaScript

Since 2015, EcmaScript6 (ES6) has introduced many developments in JavaScript coding practices. Quite a few fashionable ideas have been launched to JavaScript, enormously bettering the coding expertise. On this article, you will find out about iterators and turbines in JavaScript.

Iterators and turbines are two totally different ideas, however they’re utilized in comparable methods. They’re used to iterate by arrays and objects in JavaScript.


Iterators are like superior loops that may be paused. Iterators include the subsequent() perform, which returns the worth and the performed standing. The worth area is the worth within the array at a given index. performed is the boolean worth that returns the standing of completion for the iteration by the loop.

This is an instance that demonstrates how iterators:

perform fruitIter(fruits){
let index = 0;
return {
subsequent: perform(){
return index < fruits.size ? { worth: fruits[index++], performed: false } : {performed: true}
const fruitsArray = ["Mango", "Banana", "Grapes"];
const fruits = fruitIter(fruitsArray);



Whenever you cross the fruitsArray array within the fruitIter() technique, it returns an iterator that is saved within the fruits variable. The index variable within the fruitIter() technique will get initialized to 0. This technique returns the subsequent() perform that helps in looping through the array. The subsequent() perform checks whether or not the index is lower than fruitsArray size. In that case, it returns two variables: the fruit identify at that index, and the performed standing. Whereas returning these values, it additionally increments the index worth.

To verify how this technique works and print the fruit identify, you could name the subsequent() perform on the fruits iterator and get entry to its worth.

Associated: What Is a Function in Programming?


Turbines are much like Iterators however they return a number of values. These values are referred to as yield values. Generator capabilities are written utilizing the perform* syntax. * denotes that it is not a traditional perform, however a generator. This is an instance of turbines:

perform* printFruits(){
yield "Mango";
yield "Banana";
yield "Grapes";
const fruit = printFruits();


{worth: 'Mango', performed: false}

On this instance, yield is the iterator. Whenever you name the perform printFruits(), and print fruit.subsequent(), it provides you an object the place you get the worth. The performed standing denotes whether or not all values have been iterated by.

Study Knowledge Constructions Utilizing ES6 Lessons in JavaScript

JavaScript ES6 introduced plenty of development in coding practices. Not the least of which is constructing information constructions utilizing ES6 lessons. Push. pop, and stack your solution to the highest and turn out to be a JavaScript professional!

A cardboard box
How To Build Data Structures With JavaScript ES6 Classes

With the introduction of JavaScript ES6 came custom data structures. Here’s how to create and apply them.

Read Next

About The Creator | An Introduction to Iterators and Turbines in JavaScript


Fry Electronics is an automatic aggregator of the all world’s media. In each content, the hyperlink to the primary source is specified. All trademarks belong to their rightful owners, all materials to their authors. If you are the owner of the content and do not want us to publish your materials, please contact us by email – The content will be deleted within 24 hours.

Related Articles

Leave a Reply

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

ten − 2 =

Back to top button