JS: Iterator.prototype.find

By Xah Lee. Date: . Last updated: .

(new in ECMAScript 2025)

iterator.find(f)
  • Return the first yield of iterator where the function f returns true.
  • Return undefined if not found.

Normally you can use this method on any Iterable, because all standard iterable objects are also Iterator.

f is passed args: currentElement, currentIndex.

similar to Array.prototype.find

// define a generator function
function* gf() {
  for (let x of [5, 3, 4, 3]) yield x;
}

// get the first even number
console.log(gf().find((x, i) => (x % 2 === 0)) === 4);
// true

// get the first yield that is 9
console.log(gf().find((x, i) => (x === 9)) === undefined);
// true

JavaScript. iterator helpers