Array.prototype.every
The every() method tests whether
all elements in the array pass the test implemented by the provided function. It
returns a Boolean value.
Syntax
// Arrow function
every((element) => { /* … */ })
every((element, index) => { /* … */ })
every((element, index, array) => { /* … */ })
// Callback function
every(callbackFn)
every(callbackFn, thisArg)
// Inline callback function
every(function (element) { /* … */ })
every(function (element, index) { /* … */ })
every(function (element, index, array) { /* … */ })
every(function (element, index, array) { /* … */ }, thisArg)
Parameters
callbackFn: A function to execute for each element in the array. It should return a truthy value to indicate the element passes the test, and a falsy value otherwise.
The function is called with the following arguments:
element- : The current element being processed in the array.
index- : The index of the current element being processed in the array.
array- : The array
every()was called upon.
- : The array
thisArgoptional- : A value to use as
thiswhen executingcallbackFn.
- : A value to use as
Return value
true if callbackFn returns a truthy value for every array element. Otherwise, false.
Description
The every() method is an iterative method. It calls a provided callbackFn function once for each element in an array, until the callbackFn returns a falsy value. If such an element is found, every() immediately returns false and stops iterating through the array. Otherwise, if callbackFn returns a truthy value for all elements, every() returns true.
every acts like the "for all" quantifier in mathematics. In particular, for an empty array, it returns true. (It is vacuously true that all elements of the empty set satisfy any given condition.)
callbackFn is invoked only for array indexes which have assigned values. It is not invoked for empty slots in sparse arrays.
every() does not mutate the array on which it is called, but the function provided as callbackFn can. Note, however, that the length of the array is saved before the first invocation of callbackFn. Therefore:
callbackFnwill not visit any elements added beyond the array's initial length when the call toevery()began.- Changes to already-visited indexes do not cause
callbackFnto be invoked on them again. - If an existing, yet-unvisited element of the array is changed by
callbackFn, its value passed to thecallbackFnwill be the value at the time that element gets visited. Deleted elements are not visited.
The every() method is generic. It only expects the this value to have a length property and integer-keyed properties.