Array.every()
Checks if all elements pass a test (returns true/false).
array.every(predicateFn, thisArg?)This static page keeps the syntax and examples indexed for search, while the coding app handles interactive exploration and saved references.
What it does
Overview
Checks if all elements pass a test (returns true/false).
Array.prototype.every() is a high-order iterative method used to verify if every element in an array satisfies a specific condition defined by a predicate function. It returns true if the callback function returns a truthy value for every array element, and false otherwise. A key performance characteristic is its short-circuiting behavior: the method stops iterating and immediately returns false as soon as a single element fails the test. This makes it significantly more efficient than using forEach() for validation tasks. For empty arrays, every() returns true regardless of the condition (a mathematical concept known as vacuous truth), which is a frequent source of logic errors. The method does not mutate the array on which it is called, and it is not invoked for empty slots in sparse arrays. In terms of complexity, it is O(n) in the worst case (when all elements pass) and O(1) in the best case (when the first element fails).
Quick reference
Syntax
array.every(predicateFn, thisArg?)
Inputs
Parameters
See it in practice
Examples
Validating Numeric Ranges
const ages = [21, 18, 42, 33];
const allAdults = ages.every(function(age) {
return age >= 18;
});true
Checks if every number in the array is greater than or equal to 18.
Verifying Object Properties in an Array
const users = [
{ id: 1, active: true },
{ id: 2, active: true },
{ id: 3, active: false }
];
const isSystemOnline = users.every(user => user.active);false
Iterates through user objects to ensure the 'active' property is truthy for all entries.
Vacuous Truth with Empty Arrays
const emptyList = [];
const result = emptyList.every(x => x > 100);true
Calling every() on an empty array always returns true, because there are no elements that fail the condition.
Reliable Date Comparisons
const schedule = ['2023-10-01', '2023-10-15', '2023-10-20'];
const cutoff = new Date('2023-11-01').getTime();
const allBeforeCutoff = schedule.every(dateStr => {
return new Date(dateStr).getTime() < cutoff;
});true
Uses getTime() for reliable numeric ordering when comparing dates within the predicate.
Debug faster
Common Errors
LogicError
Cause: Expecting false for an empty array.
Fix: Check the array length explicitly if you need to ensure the array is not empty before validation.
const items = [];
const isValid = items.length > 0 && items.every(i => i.active);TypeError
Cause: Forgetting the return statement in the predicate function.
Fix: Ensure the callback returns a truthy value; otherwise, it returns undefined, which is coerced to false.
const result = [1, 2, 3].every(num => {
num > 0; // Missing 'return' keyword
});Runtime support
Compatibility
Source: MDN Web Docs
Common questions
Frequently Asked Questions
Checks if all elements pass a test (returns true/false).
predicate: Returns true if the element is valid. thisArg: Value used as 'this' inside predicate.
LogicError: Check the array length explicitly if you need to ensure the array is not empty before validation. TypeError: Ensure the callback returns a truthy value; otherwise, it returns undefined, which is coerced to false.