Array.prototype.some
Checks whether at least one element passes a test.
Available since: ES5
Syntax
array.some(callbackFn)Parameters
| Parameter | Type | Description |
|---|---|---|
| callbackFn | Function | A test function for an element. |
Return value
boolean`true` if at least one element passed the test.
Examples
Is there a negative number:
console.log([3, -1, 5].some((n) => n < 0));Output:
trueThe companion every() checks that all elements match:
console.log([3, -1, 5].every((n) => n > 0));Output:
falseNotes
For an empty array some() returns false and every() returns true.
See also
- Array.prototype.find — Returns the first element that passes a test.
- Array.prototype.includes — Checks whether an array contains a value.
- Array.prototype.every — Checks that every element of an array passes a test.
Related recipes
In the tutorial
- Searching arrays — How to find an element in a JavaScript array: includes, indexOf, find, findIndex, some and every.