Arrays have had map, filter and reduce for as long as most of us have been writing JavaScript. But a lot of JavaScript values are not arrays. Map.prototype.entries(), Set.prototype.values(), String.prototype.matchAll() and every generator function return iterators, and until recently iterators had none of those methods. The usual workaround was to spread everything into an array first, which allocates a copy and throws away any chance of stopping early.
Iterator helpers fix that. They add familiar methods directly to iterators, and they work lazily, one value at a time. This article is for JavaScript developers who are comfortable with array methods and want to know when the iterator versions are the better tool. We will cover what an iterator is, the methods you get, how laziness changes things, and where support stands.
A quick refresher on iterators
#An iterator is an object with a next() method that returns objects shaped like { value, done }. You rarely call next() yourself. for...of, spread syntax and Array.from all do it for you.
_10const roles = new Map([_10 ["ada", "admin"],_10 ["grace", "editor"],_10 ["linus", "viewer"],_10]);_10_10const entries = roles.entries();_10entries.next(); // { value: ["ada", "admin"], done: false }
Built-in iterators inherit from Iterator.prototype. That shared prototype is where the new helper methods live, so every built-in iterator and every generator gets them automatically.
The methods
#There are two groups. The first returns a new iterator, so you can keep chaining:
map(fn): transforms each value.filter(fn): keeps values wherefnreturns a truthy value.take(n): yields at mostnvalues, then stops.drop(n): skips the firstnvalues.flatMap(fn): maps each value to an iterator or iterable and flattens one level.
The second group consumes the iterator and returns a result:
toArray(): collects the values into an array.reduce(fn, initial): folds the values into one.forEach(fn): runsfnfor each value.some(fn),every(fn)andfind(fn): stop as soon as the answer is known.
There is also a static Iterator.from(), covered below.
Callbacks receive the value and a counter, the zero-based position of the value in the sequence, much like the index argument in array methods.
_10const editors = roles_10 .entries()_10 .filter(([, role]) => role !== "viewer")_10 .map(([name]) => name)_10 .toArray();_10_10// ["ada", "grace"]
Compare that with the older version, which builds an intermediate array:
_10const editors = [...roles.entries()]_10 .filter(([, role]) => role !== "viewer")_10 .map(([name]) => name);
For three entries the difference is invisible. For a large map, or a source that produces values slowly, it starts to matter.
Laziness
#Array methods are eager. Each step runs over the whole array and builds a new one before the next step starts. Iterator helpers are lazy. Nothing happens until something asks for a value, and then each value travels through the whole chain before the next one is pulled.
_16function* naturals() {_16 let n = 1;_16 while (true) {_16 console.log("producing", n);_16 yield n++;_16 }_16}_16_16const firstSquares = naturals()_16 .filter((n) => n % 2 === 0)_16 .map((n) => n * n)_16 .take(3)_16 .toArray();_16_16console.log(firstSquares); // [4, 16, 36]_16// "producing" is logged six times, not forever
naturals() never ends, yet this finishes, because take(3) stops pulling after three values reach it. With arrays you cannot even start, since you would need to build an infinite array first.
Laziness also means a chain on its own does nothing. This line runs no callbacks at all:
_10const pending = naturals().map((n) => n * 2);
Work happens only when you consume pending with toArray(), for...of, spread or one of the other consuming methods.
Real-world uses
#Filtering a Map without converting it
The Map constructor accepts any iterable of entries, and helper iterators are iterable, so you can filter a map into a new map in one expression.
_10const stock = new Map([_10 ["apples", 12],_10 ["pears", 0],_10 ["plums", 4],_10]);_10_10const inStock = new Map(stock.entries().filter(([, count]) => count > 0));_10// Map { "apples" => 12, "plums" => 4 }
Working with regular expression matches
matchAll returns an iterator of match objects, which pairs nicely with map.
_10const text = "Release notes for #typescript and #javascript";_10_10const tags = text_10 .matchAll(/#(\w+)/g)_10 .map((match) => match[1])_10 .toArray();_10_10// ["typescript", "javascript"]
Taking the first few results from an expensive source
Generators are a natural way to describe paged data. Iterator helpers let callers decide how much they need.
_10function* chunks(items, size) {_10 for (let i = 0; i < items.length; i += size) {_10 yield items.slice(i, i + size);_10 }_10}_10_10const firstTwoBatches = chunks(bigList, 100).take(2).toArray();
Only the first two slices are ever made, however long bigList is.
DOM collections
NodeList has a values() method that returns an iterator, so you can chain helpers onto the result of querySelectorAll.
_10const missingAlt = document_10 .querySelectorAll("img")_10 .values()_10 .filter((img) => !img.alt)_10 .toArray();
Iterator.from
Some objects implement the iterator protocol by hand without inheriting from Iterator.prototype, so they do not have the helpers. Iterator.from() wraps them. It also accepts any iterable, such as an array or a string.
_10const countdown = {_10 n: 3,_10 next() {_10 return this.n > 0 ? { value: this.n--, done: false } : { value: undefined, done: true };_10 },_10};_10_10Iterator.from(countdown).map((n) => `${n}...`).toArray();_10// ["3...", "2...", "1..."]
Things to watch for
#- Iterators are single use. Once you consume one, it is finished. Calling
toArray()twice on the same chain gives you the values the first time and an empty array the second. If you need several passes, collect into an array once. - There is no length, index access, sort or reverse. Those need all the values at once. Use
toArray()and then array methods. - take and drop validate their argument. Passing a negative number or
NaNthrows aRangeError. - Stopping early closes the source. When
takehas enough values, orfindgets a match, the helper callsreturn()on the underlying iterator, so a generator'sfinallyblock runs and resources can be released. - Async iterators are not covered. These helpers only work on synchronous iterators. Async iterator helpers are a separate proposal and are not widely available yet. For async sources, use
for await...oforArray.fromAsync.
Browser support
#Iterator helpers are Baseline newly available since 31 March 2025, when Safari shipped them. The first supporting versions are:
- Chrome and Edge 122
- Firefox 131
- Safari 18.4, on macOS and iOS
On the server they are available in Node 22 and later, so every supported Node release has them. They are expected to reach Baseline widely available in late 2027. If you need to support older Safari versions before then, core-js provides a polyfill.
For TypeScript users, the type definitions for iterator helpers come with a recent lib or target setting, such as esnext. If the methods show as missing in your editor, check that your lib includes a recent ECMAScript version.
When not to use it
#- You already have an array. Array methods are well known, give you an index, and do not need a conversion step. Turning an array into an iterator just to call
mapadds nothing. - You need the data more than once. An iterator is gone after one pass. Build an array if you will sort, reverse, count or loop again.
- You need random access or the total count. Those need every value up front.
- Your audience includes older Safari without a polyfill. Until widely available status, check your analytics before relying on them in browser code.
Wrapping up
#- Every built-in iterator and generator now has
map,filter,take,drop,flatMap,reduce,toArray,forEach,some,everyandfind. - They are lazy: values flow through the chain one at a time, so you can stop early and even work with infinite sequences.
- They save you from spreading
Map,Set,matchAllandNodeListresults into arrays just to transform them. - Iterators are single use. Collect with
toArray()when you need multiple passes or array-only operations. - Supported in Chrome and Edge 122, Firefox 131, Safari 18.4 and Node 22 and later.
JavascriptWas this article helpful?

About the author
Danny Engineering
A software engineer with a strong belief in human-centric design and driven by a deep empathy for users. Combining the latest technology with human values to build a better, more connected world.