Name
Array.every() — test whether a predicate is true for every element
Availability
ECMAScript 5
Synopsis
array.every(predicate)array.every(predicate,o)
Arguments
predicateA predicate function to test array elements
oThe optional
thisvalue for invocations ofpredicate.
Returns
true if predicate returns true (or any truthy value) for every
element of array or false if the predicate returns false (or a falsy value) for any
element.
Description
The every() method tests
whether some condition holds for all elements 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 false (or any value that converts to
false), then every() stops looping and returns false immediately. If every invocation of
predicate returns true, then every() returns true. When invoked on an empty array,
every() returns true.
For each array index i,
predicate is invoked with three
arguments:
predicate(array[i],i,array)
The return value of predicate is
interpreted as a boolean value. true and all truthy values indicate that
the array element passes the test or meets the condition described
by that function. A return value of false or any falsy value means that the
array element does not pass the test.
See Array.forEach() for further details.
Example
[1,2,3].every(function(x){returnx<5;})// => true: all elts are < 5[1,2,3].every(function(x){returnx<3;})// => false: all elts are not < 3[].every(function ...
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