JavaScript Iterator filter() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Instance method

Definition

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.

JavaScript
const it = [1, 2, 3, 4].values();

const evens = it.filter((n) => n % 2 === 0);

console.log(evens.toArray());   // [2, 4]
CallWhat it does
iterator.filter(callbackFn)Yield only values for which callbackFn(value) is truthy.
iterator.filter(fn).toArray()Filter values, then collect them into an array.
[1, 2, 3].values().filter(fn)Start from an array iterator with .values().
iterator.map(fn).filter(pred)Transform first, then keep matching results.

An Iterator helper (Baseline Newly available) on Iterator.prototype. No import required. Feature-detect before use in older engines. Siblings: Iterator.concat(), Iterator.from(), Iterator.zip(), Iterator.zipKeyed(), chunks(), drop(), every(), find(), flatMap(), forEach(), includes(), join(), map(), reduce(), some(), take(), toArray(), windows(), [Symbol.dispose](), [Symbol.iterator]().

Library declarations
JavaScript
// Iterator helpers (Baseline Newly available)
Iterator.prototype.filter(callbackFn)

// callbackFn(value) : return truthy to keep the value
// returns          : a new lazy iterator helper

Parameters

ParameterDescription
callbackFnRequired. 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 ValueDescription
Iterator helperA 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().

JavaScript
const nums = [1, 2, 3, 4, 5].values();
const evens = nums.filter((n) => n % 2 === 0);

console.log(evens.toArray());
Try It Yourself

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

  1. Start from an array iterator. .values() yields 1 through 5.
  2. Test each number. The callback keeps a value when n % 2 === 0.
  3. 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);
Try It Yourself

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

  1. Keep 3 and up. 1 and 2 are dropped; 3, 4, and 5 pass.
  2. Double what remains. Those values become 6, 8, and 10.
  3. 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);
}
Try It Yourself

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

  1. Yield four words. The generator produces "hi", "hello", "ok", and "world".
  2. Keep longer words. Only strings with length greater than 2 pass the test.
  3. Print as you go. for...of pulls "hello" and "world" one at a time.

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.

2025 Standard since
Google Chrome 122+ · Desktop & Mobile
Full support
Microsoft Edge 122+ · Chromium based
Full support
Mozilla Firefox 131+ · Desktop & Mobile
Full support
Apple Safari 18.4+ · macOS & iOS
Full support
Opera 108+ · Chromium based
Full support
Samsung Internet 26+ · Android
Supported
Android WebView 122+ · Chromium based
Supported
Node.js 22+ · Server runtime
Supported
Deno Recent versions · JavaScript runtime
Supported
Bun Recent versions · JavaScript runtime
Supported
Internet Explorer No 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.

Next: find()

Learn how to stop at the first matching value from an iterator with find().

find() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

14 people found this page helpful