iteration
Array.prototype.some
Returns true if the callback returns truthy for at least one element, short-circuiting on the first hit. Reach for it to answer "does anything match?" without walking the full array.
arr.some(callbackFn[, thisArg]) Parameters
| Parameter | Purpose |
|---|---|
| callbackFn(element, index, array) | Predicate tested against each element |
| thisArg | Value used as `this` inside callbackFn |
Examples
console.log([1,2,3].some(n => n > 2)); Logs: true
console.log([1,2,3].some(n => n > 99)); Logs: false
console.log([].some(() => true)); Logs: false (empty arrays are always false)
console.log([{ok:false},{ok:true}].some(o => o.ok)); Logs: true
Gotcha
some on an empty array is false; every on an empty array is true — this is vacuous-truth by design. some short-circuits, so callbacks can have side effects that stop firing after the first match.
Related methods
Array.prototype.every
Returns true only if the callback returns truthy for every element, short-circuiting on the first falsy result. Reach for it to validate that all items meet a condition.
Array.prototype.includes
Returns true if the array contains searchElement using SameValueZero equality (ES2016). Reach for it as the readable boolean alternative to `indexOf(x) !== -1`.
Array.prototype.find
Returns the first element for which the callback returns truthy, or undefined if none match. Reach for it when you need the matching item itself, not just its index.
Array.prototype.forEach
Executes a provided callback once for each array element, in ascending order. Reach for it when you need side effects (logging, DOM updates, pushing to another store) and do not need a return value.
Array.prototype.map
Creates a new array populated with the results of calling a callback on every element of the calling array. Reach for it whenever you want to transform each element 1-to-1 into a new value.