JavaScriptArraysBeginner

Array.every()

Checks if all elements pass a test (returns true/false).

Review the syntaxStudy the examplesOpen the coding app
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

predicateFunction · Returns true if the element is valid.
thisArg (optional)any · Value used as 'this' inside predicate.

See it in practice

Examples

1

Validating Numeric Ranges

const ages = [21, 18, 42, 33];
const allAdults = ages.every(function(age) {
  return age >= 18;
});
Output:
true

Checks if every number in the array is greater than or equal to 18.

2

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);
Output:
false

Iterates through user objects to ensure the 'active' property is truthy for all entries.

3

Vacuous Truth with Empty Arrays

const emptyList = [];
const result = emptyList.every(x => x > 100);
Output:
true

Calling every() on an empty array always returns true, because there are no elements that fail the condition.

4

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;
});
Output:
true

Uses getTime() for reliable numeric ordering when comparing dates within the predicate.

Debug faster

Common Errors

1

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);
2

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

Node.js, BrowserAll modern browsers, IE9+ES5 (2009)

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.