Iterator.prototype.every()
Baseline 2025Newly available
Since March 2025, this feature works across the latest devices and browser versions. This feature might not work in older devices or browsers.
Theevery() method ofIterator instances is similar toArray.prototype.every(): it tests whether all elements produced by the iterator pass the test implemented by the provided function. It returns a boolean value.
In this article
Syntax
every(callbackFn)Parameters
callbackFnA function to execute for each element produced by the iterator. It should return atruthy value to indicate the element passes the test, and afalsy value otherwise. The function is called with the following arguments:
Return value
true ifcallbackFn returns atruthy value for every element. Otherwise,false.
Description
every() iterates the iterator and invokes thecallbackFn function once for each element. It returnsfalse immediately if the callback function returns a falsy value. Otherwise, it iterates until the end of the iterator and returnstrue. Ifevery() returnsfalse, the underlying iterator is closed by calling itsreturn() 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,every() returnsfalse as soon as the first falsy value is found. If thecallbackFn always returns a truthy value, the method never returns.
Examples
>Using every()
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().every(isEven)); // falseconst isPositive = (x) => x > 0;console.log(fibonacci().take(10).every(isPositive)); // trueconsole.log(fibonacci().every(isPositive)); // Never completesCallingevery() always closes the underlying iterator, even if the method early-returns. The iterator is never left in a half-way state.
const seq = fibonacci();console.log(seq.every(isEven)); // falseconsole.log(seq.next()); // { value: undefined, done: true }Specifications
| Specification |
|---|
| ECMAScript® 2026 Language Specification> # sec-iterator.prototype.every> |