Name
Array.some() — test whether a predicate is true for any element
Availability
ECMAScript 5
Synopsis
array.some(predicate)array.some(predicate,o)
Arguments
predicateA predicate function to test array elements
oThe optional
thisvalue for invocations ofpredicate.
Returns
true if predicate returns true (or a truthy value) for at least
one element of array or false if the predicate returns false (or a falsy value) for all
elements.
Description
The some() method tests
whether a condition holds for at least one element of an array. It
loops through the elements of array, in
ascending order, and invokes the specified
predicate function on each element in
turn. If predicate returns true (or a value that converts to true), then some() stops looping and returns true immediately. If every invocation of
predicate returns false (or a value that converts to
false), then some() returns false. When invoked on an empty array,
some() returns false.
This method is very much like every(). See Array.every() and Array.forEach() for further details.
Example
[1,2,3].some(function(x){returnx>5;})// => false: no elts are > 5[1,2,3].some(function(x){returnx>2;})// => true: some elts are > 2[].some(function(x){returntrue;});// => false: always false for []
See Also
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access