iterator.filter(callbackFn) reads each value from the source and keeps it only when callbackFn(value) is truthy. It returns a new lazy iterator of the kept values — skipped values are never yielded.
Like other helpers, the work stays lazy until you consume the result. Chain more helpers if you need to, then use toArray() or for...of to see what remains.
Remember
iterator.filter(callbackFn)
callback true → value is kept
callback false → value is skipped
returns → a new lazy iterator
Pull values with toArray() or for...of
Chain with map(), take(), and friends
Syntax
Call filter() on an iterator and pass a function that returns true for values you want to keep.
// Iterator helpers (Baseline Newly available)
Iterator.prototype.filter(callbackFn)
// callbackFn(value) : return truthy to keep the value
// returns : a new lazy iterator helper
Parameters
Parameter
Description
callbackFn
Required. Called once per value as callbackFn(value). Return a truthy result to keep the value, or a falsy result to skip it.
Return Value
Return Value
Description
Iterator helper
A new lazy iterator that yields only the kept values. Use toArray() or for...of when you need the results.
JavaScript Examples
Example 1 — Filter Even Numbers
Start with [1, 2, 3, 4, 5].values(), keep only even numbers, and collect with toArray().
The Try It page first checks that iterator.filter() exists, because Iterator helpers are still new. In an older browser it prints a short explanation and the output above. In a current browser it runs the code for real.
How It Works
Start from an array iterator..values() yields 1 through 5.
Test each number. The callback keeps a value when n % 2 === 0.
Collect what remains.toArray() builds [2, 4].
Example 2 — Chain filter() with map()
Keep large numbers first, then double them. Finish with toArray().
JavaScript
const result = [1, 2, 3, 4, 5].values()
.filter((n) => n >= 3)
.map((n) => n * 2)
.toArray();
console.log(result);
The Try It page first checks that iterator.filter() exists, because Iterator helpers are still new. In an older browser it prints a short explanation and the output above. In a current browser it runs the code for real.
How It Works
Keep 3 and up.1 and 2 are dropped; 3, 4, and 5 pass.
Double what remains. Those values become 6, 8, and 10.
Collect once.toArray() gives the final array.
Example 3 — Filter Values from a Generator
Yield a few words from a generator, keep the long ones, and print each with for...of.
JavaScript
function* words() {
yield "hi";
yield "hello";
yield "ok";
yield "world";
}
const longOnes = words().filter((w) => w.length > 2);
for (const w of longOnes) {
console.log(w);
}
The Try It page first checks that iterator.filter() exists, because Iterator helpers are still new. In an older browser it prints a short explanation and the output above. In a current browser it runs the code for real.
How It Works
Yield four words. The generator produces "hi", "hello", "ok", and "world".
Keep longer words. Only strings with length greater than 2 pass the test.
Print as you go.for...of pulls "hello" and "world" one at a time.
Compatibility
Browser & Runtime Support
Iterator.prototype.filter() is an Iterator helper (Baseline Newly available since March 2025). It works in current browsers and in Node.js 22 or newer.
✓ Baseline 2025 · Newly available
iterator.filter()
Supported in Chrome 122+, Edge 122+, Firefox 131+, Safari 18.4+, Opera 108+ and Node.js 22+. Internet Explorer never got it.
2025Standard since
Google Chrome122+ · Desktop & Mobile
Full support
Microsoft Edge122+ · Chromium based
Full support
Mozilla Firefox131+ · Desktop & Mobile
Full support
Apple Safari18.4+ · macOS & iOS
Full support
Opera108+ · Chromium based
Full support
Samsung Internet26+ · Android
Supported
Android WebView122+ · Chromium based
Supported
Node.js22+ · Server runtime
Supported
DenoRecent versions · JavaScript runtime
Supported
BunRecent versions · JavaScript runtime
Supported
Internet ExplorerNo native support
Not supported
iterator.filter()Current browsers
Bottom line: Check support with a typeof test before you use it in older browsers. Prefer chaining helpers and calling toArray() last.
Frequently Asked Questions
It returns a lazy iterator that yields only the values for which callbackFn(value) is truthy. Skipped values never appear in the result.
No. It returns another iterator helper. Use toArray() or for...of when you want a normal list of kept values.
Only when someone pulls the next value. The helper may call the callback several times to find the next kept value.
Array filter() builds a new array right away. Iterator filter() stays lazy and works on any iterator, not only arrays.
Yes. Chain helpers in any useful order, then collect once at the end with toArray() or a loop.