Array.prototype.every()
BaselineWidely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
Theevery()
method ofArray
instances tests whetherall elements in the array pass the test implemented by the provided function. Itreturns a Boolean value.
Try it
const isBelowThreshold = (currentValue) => currentValue < 40;const array1 = [1, 30, 39, 29, 10, 13];console.log(array1.every(isBelowThreshold));// Expected output: true
Syntax
every(callbackFn)every(callbackFn, thisArg)
Parameters
callbackFn
A function to execute for each element in the array. It should return atruthy value to indicate the element passes the test, and afalsy value otherwise. The function is called with the following arguments:
thisArg
OptionalA value to use as
this
when executingcallbackFn
. Seeiterative methods.
Return value
true
unlesscallbackFn
returns afalsy value for an array element, in which casefalse
is immediately returned.
Description
Theevery()
method is aniterative method. It calls a providedcallbackFn
function once for each element in an array, until thecallbackFn
returns afalsy value. If such an element is found,every()
immediately returnsfalse
and stops iterating through the array. Otherwise, ifcallbackFn
returns atruthy value for all elements,every()
returnstrue
. Read theiterative methods section for more information about how these methods work in general.
every
acts like the "for all" quantifier in mathematics. In particular, for an empty array, it returnstrue
. (It isvacuously true that all elements of theempty set satisfy any given condition.)
callbackFn
is invoked only for array indexes which have assigned values. It is not invoked for empty slots insparse arrays.
Theevery()
method isgeneric. It only expects thethis
value to have alength
property and integer-keyed properties.
Examples
Testing size of all array elements
The following example tests whether all elements in the array are 10 or bigger.
function isBigEnough(element, index, array) { return element >= 10;}[12, 5, 8, 130, 44].every(isBigEnough); // false[12, 54, 18, 130, 44].every(isBigEnough); // true
Check if one array is a subset of another array
The following example tests if all the elements of an array are present in another array.
const isSubset = (array1, array2) => array2.every((element) => array1.includes(element));console.log(isSubset([1, 2, 3, 4, 5, 6, 7], [5, 7, 6])); // trueconsole.log(isSubset([1, 2, 3, 4, 5, 6, 7], [5, 8, 7])); // false
Using the third argument of callbackFn
Thearray
argument is useful if you want to access another element in the array. The following example first usesfilter()
to extract the positive values and then usesevery()
to check whether the array is strictly increasing.
const numbers = [-2, 4, -8, 16, -32];const isIncreasing = numbers .filter((num) => num > 0) .every((num, idx, arr) => { // Without the arr argument, there's no way to easily access the // intermediate array without saving it to a variable. if (idx === 0) return true; return num > arr[idx - 1]; });console.log(isIncreasing); // true
Using every() on sparse arrays
every()
will not run its predicate on empty slots.
console.log([1, , 3].every((x) => x !== undefined)); // trueconsole.log([2, , 2].every((x) => x === 2)); // true
Calling every() on non-array objects
Theevery()
method reads thelength
property ofthis
and then accesses each property with a nonnegative integer key less thanlength
until they all have been accessed orcallbackFn
returnsfalse
.
const arrayLike = { length: 3, 0: "a", 1: "b", 2: "c", 3: 345, // ignored by every() since length is 3};console.log( Array.prototype.every.call(arrayLike, (x) => typeof x === "string"),); // true
Specifications
Specification |
---|
ECMAScript® 2026 Language Specification # sec-array.prototype.every |