Name
Array.some() — test whether a predicate is true for any element
Availability
ECMAScript 5
Synopsis
array
.
some
(
predicate
)
array
.
some
(
predicate
,
o
)
Arguments
predicate
A predicate function to test array elements
o
The optional
this
value 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
)
{
return
x
>
5
;
})
// => false: no elts are > 5
[
1
,
2
,
3
].
some
(
function
(
x
)
{
return
x
>
2
;
})
// => true: some elts are > 2
[].
some
(
function
(
x
)
{
return
true
;
});
// => false: always false for []
See Also
Get JavaScript: The Definitive Guide, 6th Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.