Chapter 10: ES6 Generators and Iterators: a Developer’s Guide

by Byron Houwens

ES6 brought a number of new features to the JavaScript language. Two of these features, generators and iterators, have substantially changed how we write specific functions in more complex front-end code.

While they do play nicely with each other, what they actually do can be a little confusing, so let’s check them out.

Iterators

Iteration is a common practice in programming and is usually used to loop over a set of values, either transforming each value, or using or saving it in some way for later.

In JavaScript, we’ve always had for loops that look like this:

for (var i = 0; i < foo.length; i++){
  // do something with i
}

But ES6 gives us an alternative:

for (const ...

Get Practical ES6 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.