Array.prototype.every()
Tests whether all elements in an array pass a callback test, short-circuiting on the first failure.
every(callbackFn)
every(callbackFn, thisArg)- Added in JavaScript ES5
Parameters
| Parameter | Type | Description |
|---|---|---|
$callbackFn | Function | A function to execute for each element. It should return a truthy value to indicate the element passes the test, and a falsy value otherwise. Called with (element, index, array). |
$thisArgoptional | any= undefined | A value to use as this when executing callbackFn. |
Return value
booleantrue unless callbackFn returns a falsy value for an array element, in which case false is immediately returned.
Errors
Nothing directly, but any exception thrown by callbackFn propagates out of every() and stops iteration.
Changelog
- PHP ES5Introduced as a standard iterative array method.
every() asks whether all elements satisfy a predicate. It iterates from index 0 and returns false the instant the callback returns a falsy value; if it survives to the end, it returns true.
When to reach for it
Reach for it in validation code where a single bad element invalidates the whole set: every field filled in, every row parsed, every response a 2xx. It expresses the intent more clearly than a loop with a flag, and because it short-circuits, it costs nothing extra on data that fails early.
What trips people up
The behaviour that surprises people most is vacuous truth: [].every(anything) is true. Mathematically that is correct — there is no element that violates the condition — but code that reads "if every item is valid, submit the form" will happily submit an empty form. Guard with a length check when emptiness is meaningful.
Holes in sparse arrays are skipped, so they never cause a failure. And as with the other iterative methods, a block-bodied arrow that forgets return yields undefined, which is falsy, so the result is always false.
Example
When to use it
- Enable a submit button only when every required field has a value
- Confirm every uploaded file is under the size limit before starting a batch
- Verify a numeric series is sorted by comparing each element to its predecessor
- Check that every API response in a batch returned a success status
Frequently asked questions
Why does every() return true on an empty array?
It returns true because no element exists that can fail the test, a convention called vacuous truth. Add an explicit arr.length > 0 check when an empty collection should not count as passing.
Does every() visit all elements?
No. It stops and returns false at the first element whose callback result is falsy, so elements after the first failure are never passed to the callback.
How is every() different from filter().length?
every() short-circuits and allocates nothing, while filter() builds a whole new array before you compare its length, which is slower and wasteful when you only need a boolean.
More examples
Form validation with an emptiness guard
Without the length > 0 guard the last call would return true and an empty form would validate. The guard is the standard fix whenever emptiness should not count as success.
function allFilled(fields) {
return fields.length > 0 && fields.every((f) => f.value.trim() !== "");
}
const filled = [{ value: "ada" }, { value: "lovelace" }];
const partial = [{ value: "ada" }, { value: " " }];
console.log(allFilled(filled)); // true
console.log(allFilled(partial)); // false
console.log(allFilled([])); // false — guard beats vacuous truthChecking whether an array is sorted
The callback uses its index argument to compare each element with the one before it, skipping the comparison at index 0. Both single-element and empty arrays count as sorted.
const isSorted = (arr) => arr.every((v, i) => i === 0 || arr[i - 1] <= v);
console.log(isSorted([1, 2, 2, 5])); // true
console.log(isSorted([1, 9, 3])); // false
console.log(isSorted([42])); // true
console.log(isSorted([])); // trueShort-circuiting on the first failure
The counter shows only three callback invocations. Iteration halts at index 2 where 5 fails, so the last two elements are never examined.
const values = [2, 4, 5, 6, 8];
let checked = 0;
const allEven = values.every((n) => {
checked++;
return n % 2 === 0;
});
console.log(allEven); // false
console.log(checked); // 3 — stopped at the odd 5
Discussion