温馨提示:本站仅提供公开网络链接索引服务,不存储、不篡改任何第三方内容,所有内容版权归原作者所有
AI智能索引来源:http://www.muo.com/javascript-iterators-generators/
点击访问原文链接

An Introduction to Iterators and Generators in JavaScript

An Introduction to Iterators and Generators in JavaScript Menu Sign in now Close Trending Submenu Productivity Android Smart TVs Networking Windows 11 Entertainment PC & Mobile Submenu Windows2 Linux Android Apple Technical Submenu Tech Explained Security Software Submenu Productivity Internet Creative Screen Submenu Entertainment Streaming Home Submenu Smart Home Home News Sign in Newsletter Menu Follow Followed Like More Action Sign in now Productivity Android Smart TVs Networking Windows 11 Entertainment Close
An Introduction to Iterators and Generators in JavaScript javascript-udemy-courses Follow Followed Like Link copied to clipboard Add us on By  Unnati Bamania Published Oct 4, 2021, 2:17 PM EDT Unnati is an enthusiastic full stack developer. She loves to build projects using various programming languages. In her free time, she loves to play the guitar and is a cooking enthusiast. Sign in to your MakeUseOf account

Since 2015, EcmaScript6 (ES6) has brought many advancements in JavaScript coding practices. Numerous modern concepts were introduced to JavaScript, greatly improving the coding experience. In this article, you'll learn about iterators and generators in JavaScript.

Iterators and generators are two different concepts, but they're used in similar ways. They're used to iterate through arrays and objects in JavaScript.

Iterators

Iterators are like advanced loops that can be paused. Iterators consist of the next() function, which returns the value and the done status. The value field is the value in the array at a given index. done is the boolean value that returns the status of completion for the iteration through the loop.

Here's an example that demonstrates how iterators:

function fruitIter(fruits){
let index = 0;
return {
next: function(){
return index 

Output:

Mango

When you pass the fruitsArray array in the fruitIter() method, it returns an iterator that's stored in the fruits variable. The index variable in the fruitIter() method gets initialized to 0. This method returns the next() function that helps in looping through the array. The next() function checks whether the index is less than fruitsArray length. If so, it returns two variables: the fruit name at that index, and the done status. While returning these values, it also increments the index value.

To check how this method works and print the fruit name, you need to call the next() function on the fruits iterator and get access to its value.

Related: What Is a Function in Programming?

Generators

Generators are similar to Iterators but they return multiple values. These values are called yield values. Generator functions are written using the function* syntax. * denotes that it's not a normal function, but a generator. Here's an example of generators:

function* printFruits(){

yield "Mango";
yield "Banana";
yield "Grapes";
}

const fruit = printFruits();
console.log(fruit.next());

Output:

{value: 'Mango', done: false}

In this example, yield is the iterator. When you call the function printFruits(), and print fruit.next(), it gives you an object where you get the value. The done status denotes whether all values have been iterated through.

Learn Data Structures Using ES6 Classes in JavaScript

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

Close
Recommended How To Build Data Structures With JavaScript ES6 Classes These Gemini commands made Android Auto much better in my car Obsession and Backrooms could change how movies get made (and it’s happened before) I turned off 4 location permissions I'd forgotten about — my battery lasts noticeably longer now Join Our Team Our Audience About Us Press & Events Media Coverage Contact Us Follow Us Advertising Careers Terms Privacy Policies MakeUseOf is part of the Valnet Publishing Group Copyright © 2026 Valnet Inc.

An Introduction to Iterators and Generators in JavaScript,AI智能索引,全网链接索引,智能导航,网页索引

    Get the hang of iterator and generator methods in JS.