Con Dividere

Main Menu

  • Normal Value
  • Quota By Country
  • Statement of Financial Accounting
  • York Antwerp Rules
  • Commerce

Con Dividere

Header Banner

Con Dividere

  • Normal Value
  • Quota By Country
  • Statement of Financial Accounting
  • York Antwerp Rules
  • Commerce
Normal Value
Home›Normal Value›An introduction to iterators and generators in JavaScript

An introduction to iterators and generators in JavaScript

By Thomas Heikkinen
October 4, 2021
0
0

Since 2015, EcmaScript6 (ES6) has brought many advancements in JavaScript coding practices. Many modern concepts have been introduced into JavaScript, greatly improving the coding experience. In this article, you will learn about iterators and generators in JavaScript.

Iterators and generators are two different concepts, but they are used in a similar way. They are used to browse arrays and objects in JavaScript.

Iterators

Iterators are like advanced loops that can be paused. The iterators are made up of Following() function, which returns the value and the completed state. The value field is the value in the array at a given index. ended is the Boolean value that returns the completion status of the iteration in the loop.

Here is an example that shows how the iterators:

function fruitIter(fruits){
let index = 0;
return {
next: function(){
return index < fruits.length ? { value: fruits[index++], done: false } : {done: true}
}
}
}
const fruitsArray = ["Mango", "Banana", "Grapes"];
const fruits = fruitIter(fruitsArray);
console.log(fruits.next().value);

Go out:

Mango

When you pass the fruit table table in the fruitIter () method, it returns an iterator which is stored in the fruits variable. The index variable in the fruitIter () The method is initialized to 0. This method returns the Following() function that helps to loop through the array. The Following() the function checks if the index is less than fruit table length. If so, it returns two variables: the name of the fruit at this index and the ended status. When returning these values, it also increments the value of the index.

To check the operation of this method and print the name of the fruit, you must call the Following() function on the fruits iterator and access its value.

Related: What is a Function in Programming?

Generators

Generators are similar to iterators, but they return multiple values. These values ​​are called efficiency values. Generating functions are written using the function * syntax. * indicates that this is not a normal function, but a generator. Here is an example of generators:

function* printFruits(){
yield "Mango";
yield "Banana";
yield "Grapes";
}
const fruit = printFruits();
console.log(fruit.next());

Go out:

{value: 'Mango', done: false}

In this example, yield is the iterator. When you call the function printFruits (), and print fruit.next (), this gives you an item that you get the value of. The ended status indicates whether all values ​​have been iterated.

Learn Data Structures Using ES6 Classes in JavaScript

JavaScript ES6 has brought a lot of advancement in coding practices. Not least is building data structures using ES6 classes. To push. pop, and stack your way to the top and become a JavaScript pro!


A cardboard box
How to create data structures with JavaScript ES6 classes

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

Read more


About the Author

Unnati Bamania
(6 published articles)

Unnati is an enthusiastic full stack developer. She enjoys building projects using various programming languages. In her spare time, she enjoys playing the guitar and is passionate about cooking.

More Unnati Bamania

Subscribe to our newsletter

Join our newsletter for technical tips, reviews, free ebooks and exclusive offers!

Click here to subscribe


Source link

Related posts:

  1. It’s Time to Rethink Your Global Logistics
  2. Xtra-Gold Sources Pronounces 2021 Regular Course Tender Provide and 2020 Provide Outcomes
  3. The brand new assortment from Payal Singhal is brilliant, daring and primarily based on the brand new commonplace
  4. 2021 Lexus ES250 AWD First Take a look at: Accessible Luxurious

Recent Posts

  • FENNEC PHARMACEUTICALS INC. Management report and analysis of the financial situation and operating results. (Form 10-Q)
  • Stocks rebound after inflation moderates slightly, giving investors hope that prices may have peaked
  • Shipping industry reports 54 losses in 2021 amid challenges: Allianz
  • Once-Abundant Atlantic Cod Trait Hits New Low
  • Metro Bus restores express lanes and adds free service to parks | Local News

Archives

  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021
  • May 2021
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • March 2020
  • April 2019
  • June 2018
  • May 2018
  • June 2016
  • May 2016
  • November 2015

Categories

  • Commerce
  • Normal Value
  • Quota By Country
  • Statement of Financial Accounting
  • York Antwerp Rules
  • Terms and Conditions
  • Privacy Policy