Array.prototype.every()

The universal check. Its one genuine trap is the empty array: every() on nothing is true, which turns "all items are valid" into "there were no items" without saying so.

Array methodES5 (2009)Live demo
Common call
items.every(x => x.valid)
Returns
boolean — stops at the first failure
Replaces
filter(...).length === items.length
Watch out
an empty array is TRUE — validate the length separately
array.every(callback[, thisArg])
→ boolean

Demo

Live evaluation
Try:
Inputs
itemsnumber[]numbers, comma separated
thresholdnumberare all above this?
Output
[1, 2, 3].every(x => x > 0)
true

True only when every element passes, and iteration stops at the first failure. The last case is the one to remember: an EMPTY array returns true, even with a threshold nothing could satisfy. That is vacuous truth — there is no element that fails, so the claim holds. It means a validation like "every field is filled in" silently passes when there are no fields at all.

Parameters

NameTypeRequiredDescription
callbackFunctionyesCalled as callback(element, index, array). Iteration stops the moment it returns falsy.
thisArganyno (undefined)Value of `this` inside the callback. Ignored for arrow functions.

Return value

boolean — True if the callback returns truthy for every element. TRUE for an empty array, because no element fails.

Common patterns

Validate a whole collection
The canonical use — but guard the empty case.
const ok = fields.length > 0 && fields.every(f => f.valid);
Check a type invariant
Confirming every element is what you expect.
const allNumbers = values.every(v => typeof v === 'number');
Express "none match"
A negated predicate, often clearer than !some.
const noneFailed = results.every(r => !r.error);

Examples

1. All pass
[1, 2, 3].every(x => x > 0)
Returns
true
2. One fails
[1, 2, 3].every(x => x > 2)
Returns
false
3. Empty is TRUE
[].every(x => false)
Returns
true
4. some disagrees
[].some(x => true)
Returns
false
5. Missing return
[1, 2, 3].every(x => { x > 0 })
Returns
false
6. Short-circuits
[0, 1, 2].every(x => x > 0)
Returns
false // stops at 0

Pitfalls

1. An empty array returns true
The trap worth knowing. "Are all of these valid?" over an empty list is true, so a validation that should have caught "nothing was submitted" passes instead. It is logically correct and practically dangerous.
Empty passes validation
[].every(f => f.valid)
true
Require content too
fields.length > 0 && fields.every(f => f.valid)
false when empty
2. A braced callback with no return always gives false
The callback yields undefined, which is falsy, so the first element fails and every reports false immediately — the opposite of the missing-return failure in some.
No return
[1, 2, 3].every(x => { x > 0 })
false
Return it
[1, 2, 3].every(x => x > 0)
true
3. Confusing !every with some
!every(p) means "at least one FAILS p", not "at least one passes p". Negating the wrong side of the pair is a subtle logic bug that only shows on mixed data.
Not the same
![1, 2].every(x => x > 1)
true — one fails, not one passes
Say it directly
[1, 2].some(x => x <= 1)
true — states the real question

When to use

Use it
  • Validating that a whole collection satisfies a rule
  • Checking a type or shape invariant across an array
  • Expressing "none of them failed"
Reach for something else
  • The array may be empty and that should fail → check the length too
  • You need at least one match → some
  • You want the failing element → find with a negated predicate

Notes

Complexity
O(n) worst case; stops at the first falsy result
Return
A boolean; the array is never modified
CPython impl
V8: Builtins-array-every.tq
Memory
No allocation
Thread-safe
Single-threaded; mutating the source inside the callback is undefined behaviour for unvisited indices

FAQ

Vacuous truth: the claim "every element satisfies P" is only falsified by an element that does not, and an empty array has none. It is the standard convention in logic and in every language with an all() function — Python's all([]) is True for the same reason.

[].every(x => false)   // true

History

ES5
every and some standardised together in 2009.