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.
Demo
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
| Name | Type | Required | Description |
|---|---|---|---|
| callback | Function | yes | Called as callback(element, index, array). Iteration stops the moment it returns falsy. |
| thisArg | any | no (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
const ok = fields.length > 0 && fields.every(f => f.valid);
const allNumbers = values.every(v => typeof v === 'number');
const noneFailed = results.every(r => !r.error);
Examples
Pitfalls
[].every(f => f.valid)
fields.length > 0 && fields.every(f => f.valid)
[1, 2, 3].every(x => { x > 0 })
[1, 2, 3].every(x => x > 0)
![1, 2].every(x => x > 1)
[1, 2].some(x => x <= 1)
When to use
- Validating that a whole collection satisfies a rule
- Checking a type or shape invariant across an array
- Expressing "none of them failed"
- 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
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