Array every() method
every() checks that all elements of an array satisfy the condition defined by the callback you pass in. If every element returns true, the method returns true; if at least one element does not match, it returns false and stops checking the rest.
Theory
TL;DR
every()returnstrueonly when all elements satisfy the condition.- Iteration stops at the first
falsereturned by the callback. - The method does not modify the source array and always returns a boolean.
- For an empty array the result is
true, a well known quirk that interviewers like to ask about. some()means "at least one",every()means "all".
Quick example
Let us check whether an array consists of even numbers only.
const numbers = [2, 4, 6, 8];
const allEven = numbers.every(num => num % 2 === 0);
console.log(allEven); // trueAdd one odd number and the result immediately becomes false:
[2, 4, 5, 8].every(n => n % 2 === 0); // falseSignature and key points
array.every((element, index, array) => {
// return true or false
});Worth saying out loud in an interview:
every()does not modify the source array.- As soon as an element makes the callback return
false, execution stops. - The method returns a boolean, not an element and not an array.
- It works with the logical result of the callback, so any returned value is treated as truthy or falsy.
- For an empty array
every()returnstrue.
An example from real development
Checking whether all users have passed verification.
const users = [
{ name: 'Alex', verified: true },
{ name: 'John', verified: true },
{ name: 'Kate', verified: false },
];
const allVerified = users.every(u => u.verified);
console.log(allVerified); // falseIteration stops at Kate, because it is already clear that the "everyone is verified" condition is broken.
Why an empty array returns true
every() verifies the statement "all elements satisfy the condition". If there are no elements, there is no element that breaks the condition either, so the result is considered true by default.
console.log([].every(x => x > 0)); // trueThis property is called vacuous truth. In practice it means that every() often needs a separate non emptiness check in front of it, otherwise "all line items are paid" is true for an order with no line items at all.
The difference between every() and some()
some()requires at least one element to match the condition.every()requires all elements to match the condition.- If
every()returnedtrue, thensome()with the same condition returnstrueon a non empty array, but on an empty arrayevery()givestruewhilesome()givesfalse. - If
some()returnedfalse, it means no element matches, soevery()with the same condition is alsofalseon a non empty array.
const ages = [18, 22, 30, 16];
console.log(ages.every(age => age >= 18)); // false, not everyone is an adult
console.log(ages.some(age => age < 18)); // true, there is at least one minorCommon mistakes
1. Forgetting about the empty array. A validation such as items.every(i => i.isValid) will happily accept an empty list as valid. Add a items.length > 0 check when that matters.
2. A missing return in a callback with curly braces. The callback returns undefined, which is falsy, so the result is always false.
arr.every(num => { num > 5 }); // always falseThe fix: arr.every(num => num > 5) or arr.every(num => { return num > 5 }).
3. Expecting every() to hand back an element or an array. The method returns only true or false; to get an element use find(), to get a subset use filter().
4. Confusing it with some(). Swapping one method for the other raises no error, it silently inverts the logic of the check, and the bug is usually noticed in production.
Short Answer
Interview readyA concise answer to help you respond confidently on this topic during an interview.