Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

JavaScript · Function

every

Array.prototype.every() tests array elements with a predicate and returns true only if every visited element passes. It stops at the first failure and returns false; an empty array returns true.

How it works

Call every() with a predicate function. JavaScript tests array elements in order and stops as soon as the predicate returns a falsy value; it returns true only when no tested element fails. Sparse-array holes are skipped, and the method can work with array-like objects that provide a length and indexed properties.

Syntax

every(callbackFn)
every(callbackFn, thisArg)

Parameters

NameTypeDescription
callbackFnfunctionFunction called for each eligible element; it should return a truthy value for a pass and a falsy value for a failure. It receives the element, index, and array.
thisArganyOptional value used as this when calling callbackFn.

Returns

true unless the callback returns a falsy value for an element; then false is returned immediately.

Key points

  • The method answers whether all visited elements satisfy a supplied test.
  • A predicate is invoked for each eligible element, in traversal order.
  • Failure short-circuits the scan, avoiding unnecessary later callback calls.
  • Only present indexes are tested; absent sparse-array entries are ignored.
  • The method can be borrowed by array-like values with the required indexed structure.
  • A borrowed call needs a length and integer-indexed properties.

Common mistakes

  • An empty array produces true, because there is no element that violates the predicate.
  • Holes in sparse arrays are skipped rather than passed to the callback, so they cannot directly cause failure.
  • The callback may not run for every element: iteration short-circuits at the first falsy result.

Example: Check that all values satisfy a condition

const scores = [80, 92, 75];
const allPassed = scores.every((score) => score >= 60);

console.log(allPassed); // true

Checks whether every score meets the passing threshold.

When to use it

All-items validation

Validate form fields, permissions, or configuration entries before proceeding.

Range verification

Check that numeric data stays within a required range.

Record consistency checks

Confirm that every record has required properties or meets a business rule.

Interview questions

QWhat does Array.prototype.every() return?

every() returns a boolean. It returns false immediately when the predicate is falsy for an element; otherwise it returns true after all applicable elements pass.

Follow-up: What happens with []? It returns true, which corresponds to the universal condition having no counterexample.

QWhat arguments can the predicate receive?

The callback receives the current element, its index, and the array being traversed. An optional thisArg supplies the callback's this value.

Follow-up: Does the callback visit a sparse array's holes? No; only indexes with assigned values are visited.

QCan every() operate on non-array objects?

The method is generic: it only requires a length property and integer-keyed properties on its this value. Therefore, suitable array-like objects can use the method through call or apply.

Follow-up: How does it differ from TypedArray.prototype.every()? The typed-array method uses the same algorithm but is not generic and only applies to typed-array instances.

  • Array.prototype.some()
  • Array.prototype.filter()
  • Array.prototype.find()
  • Array.prototype.forEach()
  • TypedArray.prototype.every()

Source: Array.prototype.every() - JavaScript - MDN Web Docs. Checked 2026-10-15.