The JavaScript Array every() method checks whether every element in an array passes a test. It calls a predicate for each present element and returns one Boolean: true if all tested elements pass, or false as soon as one fails. The original array is not changed by the method. This guide adds practical validation examples and explains empty arrays, sparse slots, asynchronous checks, and how every() compares with related methods.
Syntax and Formal Specification
The method signature is every(callbackFn, thisArg).
const ages = [25, 30, 22, 28]; const allAdults = ages.every(age => age >= 18); console.log(allAdults); // true
Parameters
callbackFn— Required. A function called for each present array element. It receives:element— The current element being tested.index— The current element’s index.array— The array on whichevery()was called.
thisArg— Optional. The value used asthisinside a regular callback function. Arrow functions ignorethisArgbecause they use lexicalthis.
Return Value
trueif the callback returns a truthy value for every element (including the case of an empty array).falseas soon as the callback returns a falsy value for any element.
Important vacuous truth: every() on an empty array returns true without ever invoking the callback. This follows the mathematical convention that “all elements of the empty set satisfy P” is trivially true:
console.log([].every(x => false)); // true (vacuous truth) console.log([].every(x => true)); // true console.log([].every(x => x > 100)); // true
This behavior often surprises developers but is correct according to the specification. It means every() is dangerous for empty-array validation unless explicitly guarded.
Immutability and the Validation Contract
every() does not mutate the array. It only reads elements and returns a boolean. This makes it safe to use on frozen arrays and in pure-functional contexts.
"use strict"; const frozen = Object.freeze([2, 4, 6, 8]); const allEven = frozen.every(n => n % 2 === 0); console.log(allEven); // true console.log(frozen); // [2, 4, 6, 8]
Note on Mutation Inside the Callback
every() captures the array length before it starts and checks present indexes below that length. Appended elements beyond the original length are not visited; changing or deleting a later element can affect what the callback sees at that index. Avoid mutating the array inside the predicate so validation uses a consistent list.
// Anti-pattern: do not mutate during every() const arr = [1, 2, 3, 4]; arr.every(n => { if (n === 2) arr.push(99); // pushed beyond the initial array length, so it is not visited return n < 10; });
Performance Characteristics
every() runs in O(n) time in the worst case, but short-circuits at the first failure. Its effective cost depends heavily on where the first failing element appears. When the array fails the predicate early, every() completes near-instantly; when the array passes the predicate universally (or fails only at the end), it visits every element.
const big = new Array(1_000_000).fill(2); big[999_999] = 3; const t1 = performance.now(); big.every(n => n % 2 === 0); // fails at the last element, full scan console.log(performance.now() - t1); big[0] = 3; const t2 = performance.now(); big.every(n => n % 2 === 0); // fails at index 0, near-instant console.log(performance.now() - t2);
Callback Overhead
Each element invokes a user-defined function. For hot loops over large arrays, this cost is significant compared to inline logic:
// Faster: manual loop avoids function-call overhead let allEven = true; for (let i = 0; i < big.length; i++) { if (big[i] % 2 !== 0) { allEven = false; break; } } // Slower (typically): callback invocation per element const viaEvery = big.every(n => n % 2 === 0);
For most application code, the readability of every() outweighs the micro-optimization. Reserve manual loops for performance-critical sections where profiling identifies every() as a bottleneck.
Require a non-empty array when needed
By definition, every() returns true for an empty array because there is no element that fails the predicate. If your rule requires at least one value, check the length as well as the predicate result.
function allScoresPassed(scores) {
return scores.length > 0 && scores.every(score => score >= 50);
}
console.log(allScoresPassed([72, 88])); // true
console.log(allScoresPassed([72, 40])); // false
console.log(allScoresPassed([])); // false
This combines two separate requirements: the list must contain at least one score, and every score must meet the threshold. If an empty list should count as valid for your application, keep the direct every() result instead.
Validate records with multiple conditions
A predicate can check several properties on each object. Use && when every condition must pass, and keep the rule together so the Boolean result has a clear meaning.
const applicants = [
{ name: "Ari", age: 24, email: "ari@example.com", verified: true },
{ name: "Noor", age: 17, email: "noor@example.com", verified: true }
];
const allEligible = applicants.every(applicant =>
applicant.age >= 18 &&
applicant.email.includes("@") &&
applicant.verified
);
console.log(allEligible); // false
The method stops when Noor fails the age rule, so later records do not need to be checked. For complex validation rules, extract the predicate into a named function and test it independently.
Choose between every(), some(), filter(), and find()
These methods answer different questions about an array:
every()answers whether all present elements pass a test.some()answers whether at least one present element passes.filter()returns a new array containing all matching elements.find()returns the first matching element, orundefined.
const temperatures = [18, 21, 26];
console.log(temperatures.every(t => t >= 18)); // true
console.log(temperatures.some(t => t > 25)); // true
console.log(temperatures.filter(t => t > 20)); // [21, 26]
console.log(temperatures.find(t => t > 20)); // 21
Use every() for all-of validation, some() for any-of checks, and filter() or find() when you need the matching data itself. If you need to know whether any item fails, some(item => !predicate(item)) is often clearer than negating the result of every().
Understand sparse arrays and empty slots
every() skips empty slots in a sparse array. It checks values stored at present indexes, not every numeric position up to length. As a result, a sparse array can pass a test even though some positions have no value.
const readings = [10, , 30];
console.log(readings.every(Number.isFinite)); // true
console.log(readings.length); // 3
console.log(1 in readings); // false
If each position must exist and contain a valid value, explicitly inspect the full index range before applying the predicate. For example, build a Boolean for every position and then check those results:
const allSlotsValid = Array.from(
{ length: readings.length },
(_, index) => Object.hasOwn(readings, index) &&
Number.isFinite(readings[index])
).every(Boolean);
console.log(allSlotsValid); // false
An explicit undefined is a present element and is passed to the callback; a hole is skipped. This distinction can matter in arrays created with a fixed length or modified with the delete operator.
Do not use an async predicate directly
every() does not wait for a Promise returned by an asynchronous callback. A Promise is truthy, so an async predicate can make the method return true without waiting for the checks to finish.
const ids = [1, 2, 3];
const result = ids.every(async id => await isAllowed(id));
console.log(result); // true for a non-empty array of Promises
For independent checks, run them with Promise.all() and then test the resolved Boolean values. For sequential checks that should stop at the first failure, use a loop with await.
const checks = await Promise.all(ids.map(id => isAllowed(id)));
const allAllowed = checks.every(Boolean);
This makes the asynchronous behavior explicit. Choose concurrent checks only when the work can safely run at the same time; use a sequential loop when each check depends on the previous one or when you need to limit requests.