some and every
Two methods answer questions about a whole array with a single true or false, and both stop as soon as they can.
some asks whether at least one item passes, every asks whether all of them do. Both return a boolean and exit early. On an empty array, some is false and every is true.
EXERCISE
1Asking a Question About the Whole List
some and every take the same kind of test as filter but answer with one boolean instead of a new array.
some: is there at least one?
const numbers = [1, 3, 5, 8];
console.log(numbers.some((n) => n % 2 === 0)); // true 8 is even
console.log(numbers.some((n) => n > 100)); // false none are
every: are they all?
console.log(numbers.every((n) => n > 0)); // true all positive
console.log(numbers.every((n) => n % 2 === 1)); // false 8 is not odd
On objects, which is where you will use them:
const users = [
{ name: "Ada", verified: true },
{ name: "Grace", verified: false },
];
console.log(users.some((u) => u.verified)); // true
console.log(users.every((u) => u.verified)); // false
const cart = [
{ name: "Tea", inStock: true },
{ name: "Cocoa", inStock: true },
];
if (cart.every((item) => item.inStock)) {
console.log("Ready to check out"); // this runs
}
Compared with filter, which answers a different question:
const numbers = [1, 3, 5, 8];
numbers.filter((n) => n % 2 === 0); // [ 8 ] which ones?
numbers.some((n) => n % 2 === 0); // true is there one?
numbers.every((n) => n % 2 === 0); // false are they all?
// Using filter to answer a yes/no question works and says less:
numbers.filter((n) => n % 2 === 0).length > 0; // true, the long way
When your next line is if (something.length > 0) after a filter, the question you were really asking was some. Saying it directly is shorter, stops at the first match instead of examining everything, and tells the next reader what the check is for rather than making them work it out from the comparison.
EXERCISE
2Both Stop Early
some stops at the first pass and every stops at the first failure, which matters for both speed and side effects.
some stops as soon as it finds one:
const numbers = [1, 2, 3, 4, 5];
numbers.some((n) => {
console.log(`checking ${n}`);
return n > 2;
});
// checking 1
// checking 2
// checking 3
// 4 and 5 were never examined.
every stops as soon as one fails:
numbers.every((n) => {
console.log(`checking ${n}`);
return n < 3;
});
// checking 1
// checking 2
// checking 3 <- this one failed, so it stopped
Which makes them usable as an early-exit loop:
const users = [
{ name: "Ada", role: "admin" },
{ name: "Grace", role: "editor" },
];
// "Does anyone here have admin rights?" Stops at Ada.
const hasAdmin = users.some((u) => u.role === "admin");
console.log(hasAdmin); // true
Validating a form, which is the classic use of every:
const fields = [
{ name: "email", value: "ada@example.com" },
{ name: "phone", value: "" },
];
const allFilled = fields.every((f) => f.value.trim().length > 0);
console.log(allFilled); // false
const missing = fields.filter((f) => f.value.trim().length === 0);
console.log(missing.map((f) => f.name)); // [ 'phone' ]
// every tells you whether to proceed.
// filter tells you what to complain about.
Pro Tip:
someandeveryare the only array methods in this unit that can stop before the end, apart fromfindandfindIndex. On a short list that is a detail. On a long one, or when the test is expensive, it is the difference between checking one item and checking a hundred thousand.
EXERCISE
3The Empty Array Answers
On an empty array some is false and every is true, and the second one catches people out until they see why.
The two answers:
const empty = [];
console.log(empty.some((n) => n > 0)); // false
console.log(empty.every((n) => n > 0)); // true <- surprising
Why every says true:
// every means "no item fails the test".
// An empty list has no items, so no item fails.
// This is called vacuous truth, and it is the standard
// behaviour in every language that has an "all" operation.
console.log([].every((n) => n > 100)); // true
console.log([].every((n) => false)); // true even with a test that always fails
Where it bites:
const cart = [];
if (cart.every((item) => item.inStock)) {
console.log("Ready to check out"); // runs, on an EMPTY cart
}
// Check that there is something there as well:
if (cart.length > 0 && cart.every((item) => item.inStock)) {
console.log("Ready to check out"); // correctly does not run
}
some has no such problem:
const cart = [];
if (cart.some((item) => item.inStock)) {
console.log("At least one item available"); // correctly does not run
}
A summary worth keeping:
// non-empty, all pass non-empty, some fail empty
// some(...) true true or false false
// every(...) true false true
Pro Tip: Any
everycheck that guards an action needs a length check beside it, because "all of them are fine" is technically true of nothing at all. This is not a JavaScript quirk to work around, it is the correct mathematical answer, and every language behaves this way. The fix is simply to ask both questions.
