Iterator.prototype.some()

Test whether at least one value of an iterator passes a callback, stopping at the first match.

Signature
some(callbackFn)
  • Added in JavaScript ES2025 (Baseline March 2025)

Parameters

ParameterTypeDescription
$callbackFnFunctionCalled for each element as callbackFn(element, index). Return a truthy value if the element passes the test. element is the current value and index is its zero-based position.

Return value

booleantrue as soon as callbackFn returns a truthy value for any element; false if the iterator is exhausted without one.

Errors

Throws a TypeError if callbackFn is not callable, or if called on a value that is not an object.

some() is one of the few terminal helpers that can finish on an infinite source, because it stops the moment the predicate is satisfied. It answers a yes/no question — "is there at least one?" — and pulls no more values than it needs to.

When to reach for it

  • Validating a stream: does any record fail the check?
  • Probing an unbounded generator for the first value with some property, when you only need the boolean.
  • Replacing a for...of loop whose whole body is a flag and a break.

What trips people up

It closes the iterator. After some() returns — early or not — it calls return() on the source, so the iterator is finished and the matching element is gone. If you want the value rather than a boolean, use find(), or filter(pred).next() when you also need to keep pulling afterwards.

The short-circuit only helps when a match exists. If the predicate is never satisfied on an infinite iterator, some() runs forever — it can prove "yes" early, never "no".

On an empty iterator the answer is false, matching Array.prototype.some().

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Check whether any parsed record is invalid without collecting them
  • Probe an infinite generator for a value above a threshold
  • Replace a flag-and-break loop over a Set or Map iterator

Frequently asked questions

Does some() terminate on an infinite iterator?

Only when a match exists. It returns true the instant the predicate is satisfied, so probing an endless generator for a value that eventually appears is safe. If no element ever passes, it keeps pulling forever — it can prove yes early but never no.

How do I get the matching value instead of true or false?

Use find(), which returns the element itself, or filter(pred).next().value if you want to keep pulling further matches from the same chain. some() deliberately returns only a boolean and closes the source.

Why is my iterator empty after calling some()?

Because some() closes the underlying iterator by calling its return() method when it finishes, whether it matched or not. That is specified behaviour and it lets generators run their finally blocks. If you need to keep using the sequence, collect it with toArray() first or start from a fresh iterator.

More examples

It closes the iterator behind it

some() calls return() on the source when it exits, so the iterator reports done afterwards. Use find() if you need the element that matched.

Example · javascript
function* naturals() {
  let n = 1;
  while (true) yield n++;
}

const seq = naturals();
console.log(seq.some((n) => n > 3)); // true

// The source is finished — the matching 4 is not recoverable.
console.log(seq.next()); // { value: undefined, done: true }

Counting how far it looked

Only two of the three records are examined: the predicate is satisfied at id 2 and iteration stops there. Array.prototype.some() behaves the same way, but here the untested record is never even produced by the source.

Example · javascript
let tested = 0;
const records = [
  { id: 1, ok: true },
  { id: 2, ok: false },
  { id: 3, ok: true },
];

const hasFailure = records.values().some((r) => {
  tested++;
  return !r.ok;
});

console.log(hasFailure, "after testing", tested); // true after testing 2

Discussion

  • Be the first to comment on this lesson.