Skip to content

Instantly share code, notes, and snippets.

@waynebaylor
waynebaylor / iterators.md
Created March 26, 2021 14:27
Javascript Iterators, Iterables, and Generators

Iterators

An iterator provides a way to iterate over a collection of values. One of the most common uses for an iterator is in for...of loops. A for...of loop will loop over the values given to it, but how does it know what order to use?

For example, why does this for...of loop start at index 0? Why not start at the end and go backwards?

const arrayOfFruits = ['apple', 'orange', 'banana', 'pear'];

for (const val of arrayOfFruits) {