Name

Array.every() — test whether a predicate is true for every element

Availability

ECMAScript 5

Synopsis

array.every(predicate)
array.every(predicate, o)

Arguments

predicate

A predicate function to test array elements

o

The optional this value for invocations of predicate.

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) { return x < 5; }) // => true: all elts are < 5
[1,2,3].every(function(x) { return x < 3; }) // => false: all elts are not < 3
[].every(function ...

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.