Iterator.prototype.find()

Baseline 2025
Newly available

Since March 2025, this feature works across the latest devices and browser versions. This feature might not work in older devices or browsers.

The find() method of Iterator instances is similar to Array.prototype.find(): it returns the first element produced by the iterator that satisfies the provided testing function. If no values satisfy the testing function, undefined is returned.

Syntax

js
find(callbackFn)

Parameters

callbackFn

A function to execute for each element produced by the iterator. It should return a truthy value to indicate a matching element has been found, and a falsy value otherwise. The function is called with the following arguments:

element

The current element being processed.

index

The index of the current element being processed.

Return value

The first element produced by the iterator that satisfies the provided testing function. Otherwise, undefined is returned.

Description

find() iterates the iterator and invokes the callbackFn function once for each element. It returns the element immediately if the callback function returns a truthy value. Otherwise, it iterates until the end of the iterator and returns undefined. If find() returns an element, the underlying iterator is closed by calling its return() method.

The main advantage of iterator helpers over array methods is that they are lazy, meaning that they only produce the next value when requested. This avoids unnecessary computation and also allows them to be used with infinite iterators. With infinite iterators, find() returns the first satisfying element as soon as it is found. If the callbackFn always returns a falsy value, the method never returns.

Examples

Using find()

js
function* fibonacci() {
  let current = 1;
  let next = 1;
  while (true) {
    yield current;
    [current, next] = [next, current + next];
  }
}

const isEven = (x) => x % 2 === 0;
console.log(fibonacci().find(isEven)); // 2

const isNegative = (x) => x < 0;
console.log(fibonacci().take(10).find(isNegative)); // undefined
console.log(fibonacci().find(isNegative)); // Never completes

Calling find() always closes the underlying iterator, even if the method early-returns. The iterator is never left in a half-way state.

js
const seq = fibonacci();
console.log(seq.find(isEven)); // 2
console.log(seq.next()); // { value: undefined, done: true }

Specifications

Specification
ECMAScript® 2027 Language Specification
# sec-iterator.prototype.find

Browser compatibility

desktop mobile server
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Bun
Deno
Node.js
find
Chrome – No support
Chrome 117 – 118 (Release date: 2023-09-12)
footnote Removed in 119 and later
Chrome – Full support
Chrome 122 (Release date: 2024-02-20)
footnote Full support
Edge – No support
Edge 117 – 118 (Release date: 2023-09-15)
footnote Removed in 119 and later
Edge – Full support
Edge 122 (Release date: 2024-02-23)
footnote Full support
Firefox – Full support
Firefox 131 (Release date: 2024-10-01)
footnote Full support
Opera – No support
Opera 103 – 104 (Release date: 2023-10-03)
footnote Removed in 105 and later
Opera – Full support
Opera 108 (Release date: 2024-03-05)
footnote Full support
Safari – Full support
Safari 18.4 (Release date: 2025-03-31)
footnote Full support
Chrome Android – No support
Chrome Android 117 – 118 (Release date: 2023-09-12)
footnote Removed in 119 and later
Chrome Android – Full support
Chrome Android 122 (Release date: 2024-02-20)
footnote Full support
Firefox for Android – Full support
Firefox for Android 131 (Release date: 2024-10-01)
footnote Full support
Opera Android – No support
Opera Android 78 – 78 (Release date: 2023-10-23)
footnote Removed in 79 and later
Opera Android – Full support
Opera Android 81 (Release date: 2024-03-14)
footnote Full support
Safari on iOS – Full support
Safari on iOS 18.4 (Release date: 2025-03-31)
footnote Full support
Samsung Internet – No support
Samsung Internet 24 – 24 (Release date: 2024-01-25)
footnote Removed in 25 and later
Samsung Internet – Full support
Samsung Internet 26 (Release date: 2024-06-07)
footnote Full support
WebView Android – No support
WebView Android 117 – 118 (Release date: 2023-09-12)
footnote Removed in 119 and later
WebView Android – Full support
WebView Android 122 (Release date: 2024-02-20)
footnote Full support
WebView on iOS – Full support
WebView on iOS 18.4 (Release date: 2025-03-31)
footnote Full support
Bun – Full support
Bun 1.1.31 (Release date: 2024-10-18)
footnote Full support
Deno – Full support
Deno 1.39 (Release date: 2023-12-14)
footnote Full support
Node.js – Full support
Node.js 22 (Release date: 2024-04-24)
footnote Full support

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
Has more compatibility info.

See also