Existence checks
Validate input before continuing when any item meets a rule.
JavaScript · Function
Array.prototype.some() tests whether at least one array element satisfies a callback. It returns true on the first truthy result and otherwise returns false.
Call some() with a callback that tests each array element. It returns true as soon as the callback produces a truthy result; if every tested element produces a falsy result, it returns false.
some(callbackFn)
some(callbackFn, thisArg)| Name | Type | Description |
|---|---|---|
callbackFn | function | A function called for each visited element. It should return a truthy value when the element passes the test and a falsy value otherwise; it receives the element, index, and array. |
thisArg | any | Optional value used as this when invoking callbackFn. |
A boolean: true when the callback returns a truthy value for at least one visited element; otherwise false.
some() to ask whether at least one array element passes a predicate.false.some() was invoked.const numbers = [1, 3, 4, 7];
const hasEven = numbers.some((number) => number % 2 === 0);
console.log(hasEven); // trueThe callback tests each number, and iteration stops when it finds 4.
Validate input before continuing when any item meets a rule.
Detect whether a collection contains an item with a particular property or state.
Stop predicate evaluation as soon as a matching element is found.
some() returns a boolean indicating whether at least one array element makes the callback return a truthy value. It short-circuits and returns true immediately on the first match; otherwise it returns false.
Follow-up: What happens for an empty array? It returns false, because no element can satisfy the predicate.
The callback receives the current element, its index, and the array being traversed. An optional thisArg supplies the callback's this value.
Follow-up: Does an arrow callback use thisArg? No. Arrow functions capture lexical this, so they do not rebind it from thisArg.
some() invokes the callback only for indexes that have assigned values, so sparse-array holes are skipped. It is also generic: it only requires a length property and integer-keyed properties on its this value.
Follow-up: Can it be used on an array-like object? Yes, when called generically, provided the object has the required length and integer-keyed properties.
Array.prototype.every()Array.prototype.find()Array.prototype.includes()Array.prototype.filter()