The JavaScript array.every() method
The every() method returns true only if all elements pass your test, and false as soon as one fails. [2, 4, 6].every(n => n % 2 === 0) is true. It's the "all" to some()'s "any". Note that an empty array returns true.
Overview
every() asks "do they all pass?" It runs your test against each element and returns true only if every single one passes. The moment it finds one that fails, it stops and returns false — so it short-circuits, not wasting time on the rest. Are all fields valid, are all items in stock, is everyone over 18: that's every().
It's the natural pair to some(): every() is true when all pass, some() is true when any passes. Both return a plain boolean, perfect for an if. The callback gets the familiar element, index and array arguments.
One quirk to remember: every() on an empty array returns true (vacuously — there's no element that fails). This is mathematically correct but occasionally surprising, so if an empty array should count as "not all valid" in your logic, check the length first.
Syntax
const allPass = array.every(callbackFn)
array.every((element, index, array) => {
return /* true if this element is acceptable */;
})
Parameters
The array.every() method accepts the following parameters.
| Parameter | Description |
|---|---|
callbackFn |
Test run on elements until one returns falsy. Receives (element, index, array). |
thisArg |
Optional. A value to use as this inside the callback. |
Example
<pre id="out" style="font:15px ui-monospace,monospace"></pre>
<script>
const form = ['Ada', 'ada@x.com', 'secret'];
const allFilled = form.every(field => field.trim().length > 0);
document.getElementById('out').textContent =
'All fields filled? ' + allFilled; // true
</script>
Best practices
- Use
every()for "are they all?" checks, e.g. validating that all form fields are filled. - Pair it mentally with some() ("is there any?") to cover all/any questions.
- Watch the empty-array case —
every()returnstruefor[]. - It short-circuits on the first failure, so it's efficient on large arrays.
Frequently asked questions
What does every() do in JavaScript?
true only if all elements pass the test in your callback, and false as soon as one fails.What is the difference between every() and some()?
every() is true only when all elements pass; some() is true when at least one passes.What does every() return for an empty array?
true — there's no element that could fail. Check the length first if an empty array should be treated as invalid.How do I check that all items in an array are valid?
arr.every(isValid), which returns true only when every element passes isValid.