QuickDevGuide

Home › JavaScript

Iterators & Generators — JavaScript

An iterable is any object with a Symbol.iterator method that returns an iterator — an object with next() returning { value, done }. Arrays, strings, Maps, Sets and NodeLists are iterable, which is why they work with for…of, spread and destructuring. Generators A function returns a generator: it pauses at each yield and resumes on the next next() call. This makes it easy to write lazy sequences — values are computed only when requested, so even infinite sequences are fine. Async generators async function + for await…of iterate over values that arrive over time — paginated API results, streams, or events. Iterator helpers Modern engines add lazy helpers directly on iterators: .map(), .filter(), .take(), .drop(), .toArray().

Key Takeaways