Array.prototype.some()

The existential check, and the mirror of every. It stops at the first success, so it is the cheap way to ask a yes/no question of a large array.

Array methodES5 (2009)Live demo
Common call
items.some(x => x.isActive)
Returns
boolean — stops at the first truthy result
Replaces
filter(...).length > 0, which scans everything
Watch out
an empty array is FALSE, where every would be true
array.some(callback[, thisArg])
→ boolean

Demo

Live evaluation
Try:
Inputs
itemsnumber[]numbers, comma separated
thresholdnumberis any above this?
Output
[1, 2, 3].some(x => x > 2)
true

True as soon as one element passes — the rest are never tested, which makes this cheap on large arrays. The empty case is worth holding next to every: some on an empty array is FALSE, because no element exists that could pass. every on the same empty array is TRUE. The two methods disagree precisely when there is nothing to check.

Parameters

NameTypeRequiredDescription
callbackFunctionyesCalled as callback(element, index, array). Iteration stops the moment it returns truthy.
thisArganyno (undefined)Value of `this` inside the callback. Ignored for arrow functions.

Return value

boolean — True as soon as the callback returns truthy for any element. False for an empty array — there is nothing that could satisfy it.

Common patterns

Existence check
Cheaper and clearer than counting matches.
if (errors.some(e => e.fatal)) { abort(); }
Match objects by contents
What includes cannot do.
const has = items.some(o => o.id === targetId);
Validate that nothing is broken
Often reads better than a negated every.
const invalid = fields.some(f => !f.valid);

Examples

1. One passes
[1, 2, 3].some(x => x > 2)
Returns
true
2. None pass
[1, 2, 3].some(x => x > 99)
Returns
false
3. Empty is false
[].some(x => true)
Returns
false
4. every disagrees
[].every(x => false)
Returns
true
5. Match by contents
[{id: 1}].some(o => o.id === 1)
Returns
true
6. Short-circuits
[1, 2, 3].some(x => x === 1)
Returns
true // 2 and 3 never tested

Pitfalls

1. An empty array is false
Correct, but worth stating: "is there any?" over nothing is no. It is the opposite of every, which returns true for an empty array — so swapping one for the other flips the empty case.
Assumed true
[].some(x => true)
false
Check length too
items.length > 0 && items.some(pred)
explicit
2. A braced callback with no return always gives false
Without an explicit return the callback yields undefined, which is falsy, so no element ever passes and some always answers false.
No return
[1, 2, 3].some(x => { x > 0 })
false
Return it
[1, 2, 3].some(x => x > 0)
true
3. Using some for side effects
Because it short-circuits, some is occasionally abused as a breakable forEach. It works, but it reads as a question rather than a command and the next reader will misjudge the intent.
Loop in disguise
items.some(x => { send(x); return done; })
works, but obscures intent
Use a for...of
for (const x of items) { send(x); if (done) break; }
says what it does

When to use

Use it
  • Asking whether at least one element qualifies
  • Matching objects by their contents
  • Short-circuiting a check over a large array
Reach for something else
  • You need the matching element → find
  • You need ALL elements to qualify → every
  • Matching a plain value → includes
  • You want the count → filter().length

Notes

Complexity
O(n) worst case; stops at the first truthy result
Return
A boolean; the array is never modified
CPython impl
V8: Builtins-array-some.tq
Memory
No allocation
Thread-safe
Single-threaded; mutating the source inside the callback is undefined behaviour for unvisited indices

FAQ

Because it asks whether any element satisfies the test, and an empty array has no elements — so nothing can. It is the mirror of every, which is vacuously true on an empty array for the same structural reason.

[].some(x => true)    // false
[].every(x => false)  // true

History

ES5
some and every standardised together in 2009.