JS: Iterator.prototype.every

By Xah Lee. Date: . Last updated: .

(new in JS: ECMAScript 2025)

iterator.every(f)
  • Return true if the function f return true for every yield in Iterator iterator.
  • As soon as f return false, exit the iteration and return false, and set the iterator to no more yield.

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

  • f is given args:
  • currentElement
  • currentIndex

similar to JS: Array.prototype.every

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

// check if every item is less than 5
console.log(gf().every((x) => x < 5));
// true

// check if every item is less than 3
console.log(gf().every((x) => x < 3) === false);
// true

JavaScript. iterator helpers