The JavaScript array.some() method
The some() method returns true if at least one element passes your test, and false if none do. [1, 2, 3].some(n => n > 2) is true. It stops as soon as it finds a match. Its opposite number is every(), which checks that all elements pass.
Overview
some() asks "is there at least one?" It runs your test against the elements and returns true the moment one passes — is any item out of stock, does any field have an error, is anyone an admin. If it gets to the end without a match, it returns false. Because it short-circuits on the first hit, it doesn't waste time checking the rest.
It's part of a tidy pair with every(): some() is true if any element passes, every() is true only if all of them do. Both return a plain boolean, which makes them perfect for if conditions. The callback takes the familiar element, index and array arguments.
If you want the matching element rather than just a yes/no, use find(); if you want all the matches, use filter(). And for a simple "does this exact value exist" check, includes() is more direct. Reach for some() when the question is really "does anything here meet this condition?"
Syntax
const result = array.some(callbackFn)
const result = array.some(callbackFn, thisArg)
array.some((element, index, array) => {
return /* true if this element qualifies */;
})
Parameters
The array.some() method accepts the following parameters.
| Parameter | Description |
|---|---|
callbackFn |
Test run on elements until one returns truthy. Receives (element, index, array). |
thisArg |
Optional. A value to use as this inside the callback. |
Example
<pre id="out" style="font:15px ui-monospace,monospace"></pre>
<script>
const cart = [
{ item: 'Book', inStock: true },
{ item: 'Lamp', inStock: false }
];
const anyOutOfStock = cart.some(p => !p.inStock);
document.getElementById('out').textContent =
'Any out of stock? ' + anyOutOfStock; // true
</script>
Best practices
- Use
some()for "is there at least one?" checks inside anif. - Pair it mentally with
every()("are they all?") — together they cover any/all questions. - Need the element, not a boolean? Use find(); need all matches? Use filter().
- For a plain value-presence check, includes() is simpler than
some().
Frequently asked questions
What does some() do in JavaScript?
true if at least one element passes the test in your callback, otherwise false. It stops at the first match.What is the difference between some() and every()?
some() is true if any element passes; every() is true only if all elements pass. They're the "any" and "all" of arrays.What is the difference between some() and find()?
some() returns a boolean; find() returns the first matching element itself. Use find() when you need the item.What does some() return for an empty array?
false — there's no element that could pass the test. (Note every() returns true for an empty array.)