JavaScriptArrays
Array. prototype. some()
Checks whether at least one element matches a condition.
- Returns
trueif any element passed the test, otherwisefalse.- Changes the original
- No
Example
#JavaScript
const ages = [12, 15, 19, 14]
console.log(ages.some((age) => age >= 18))
console.log(ages.some((age) => age > 30))
console.log([].some(() => true))Console
true
false
false
Definition and usage
#The some() method returns true if the testing function returns a truthy value for at least one element. It stops checking at the first match.
It’s a clear way to ask whether there’s any match at all: a discounted product in the cart, a student with an overdue assignment, a form with even one empty field.
For an empty array, some() always returns false, because there’s no element that could satisfy the condition.
Syntax
#Syntax
array.some(callbackFn)
array.some(callbackFn, thisArg)Parameters
#callbackFn
function (element, index, array)
The testing function.thisArg
optional
Thethisvalue inside the function.
Browser support
#Widely available
Works in all modern browsers, phones included. You can use it without worry.
- Chrome
- Edge
- Firefox
- Safari
Good practices
#- To check whether an array contains a specific value,
includes()is simpler. some()stops at the first match, so it’s more efficient thanfilter(...).length > 0.
Related entries
#- Array.prototype.every()Checks whether all elements match a condition.
- Array.prototype.includes()Checks whether an array contains the given value.
- Array.prototype.find()Returns the first element that matches a condition, or undefined.
- Array.prototype.filter()Creates a new array with the elements for which the function returned true.
Spotted a mistake or missing an example? Write to us.