Movatterモバイル変換


[0]ホーム

URL:


  1. Web
  2. JavaScript
  3. Reference
  4. Standard built-in objects
  5. Iterator
  6. some()

Iterator.prototype.some()

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.

Thesome() method ofIterator instances is similar toArray.prototype.some(): it tests whether at least one element produced by the iterator passes the test implemented by the provided function. It returns a boolean value.

Syntax

js
some(callbackFn)

Parameters

callbackFn

A 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:

element

The current element being processed.

index

The index of the current element being processed.

Return value

true if the callback function returns atruthy value for at least one element. Otherwise,false.

Description

some() iterates the iterator and invokes thecallbackFn function once for each element. It returnstrue immediately if the callback function returns a truthy value. Otherwise, it iterates until the end of the iterator and returnsfalse. Ifsome() returnstrue, 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,some() returnstrue as soon as the first truthy value is found. If thecallbackFn always returns a falsy value, the method never returns.

Examples

Using some()

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().some(isEven)); // trueconst isNegative = (x) => x < 0;console.log(fibonacci().take(10).some(isNegative)); // falseconsole.log(fibonacci().some(isNegative)); // Never completes

Callingsome() 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.some(isEven)); // trueconsole.log(seq.next()); // { value: undefined, done: true }

Specifications

Specification
ECMAScript® 2026 Language Specification
# sec-iterator.prototype.some

Browser compatibility

See also

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp