Skip to content
Tutorials Center
My learning

Topics

JavaScript Reference ยท Array

JavaScript Array every() method

Reference for JavaScript Array every(): test whether all elements pass a callback, with short-circuiting, empty-array behavior, sparse arrays and examples.

The every() method asks one question: does every element of the array pass a test? You give it a callback, it calls the callback for each element in order, and it returns true only if the callback returned a truthy value every time. As soon as one element fails, it stops and returns false.

Use it for validation ("are all fields filled in?"), for permission checks and for assertions about a whole list. It never changes the array.

JavaScript
[2, 4, 6].every((n) => n % 2 === 0); // true

Try it

JS All or nothing

Press Run (or Ctrl + Enter) to see the console output here.

No output.

JS Short-circuiting and arguments

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Syntax

JavaScript
every(callbackFn)
every(callbackFn, thisArg)
Name Description
callbackFn Called as callbackFn(element, index, array). A truthy return value means the element passes.
thisArg Optional. Value used as this inside callbackFn (ignored by arrow functions).

Return value: true if the callback returned a truthy value for every element, otherwise false. An empty array gives true.

Exceptions: a TypeError if callbackFn is not a function.

Usage notes

  • It short-circuits. The first falsy result ends the loop, so later elements are never visited. That makes it cheap on long arrays that usually fail early.
  • Empty arrays return true. With no elements there is nothing that can fail (mathematicians call this "vacuous truth"). If an empty list should not count as valid, check arr.length > 0 && first.
  • Holes are skipped. The callback is not called for empty slots in a sparse array, so they cannot make every() fail.
  • Mutation during iteration: the range of indexes is fixed before the first call. Elements you add later are not visited, and elements you delete before they are reached are skipped.
  • Do not use it for side effects. Returning nothing from the callback means undefined, which is falsy, so the loop stops after the first element. Use forEach() or a for...of loop for side effects.
  • Opposite question: some() asks whether at least one element passes. arr.every(fn) is equivalent to !arr.some((x) => !fn(x)).
  • It works on array-likes through Array.prototype.every.call(arrayLike, fn).

Browser support

every() is supported in every current browser and has been for a very long time.