bhylu.com

Advertisements

JS Iterators

An iterator exposes next, which returns a value and a done flag. Consumers repeat until done becomes true.

Where to run this

This example uses the JavaScript console playground. Select Run to execute it; the example does not run on this lesson page.

A practical example

JavaScript

const iterator = ["Brush", "Tape"][Symbol.iterator]();
console.log(iterator.next());
console.log(iterator.next());
console.log(iterator.next());

What happens

The first two results hold values; the third reports done.