Arrays
some() and every()
Answer the questions "is there at least one?" and "are they all?".
Why it exists
Many checks sound like "at least one" or "all": is anything in the cart out of stock, are all form fields filled, does the user have unread notifications. With a loop that's a flag, a break and careful logic. some and every turn such a check into one line that reads almost like an English sentence.
Where it's used
- Form validation:
fields.every((f) => f.value !== ""). - The "Place order" button:
cart.some((item) => !item.inStock)— disable it. - Permissions:
user.roles.some((r) => r === "admin"). - Data checks:
rows.every((row) => row.length === 9).
Syntax
array.some((item) => condition)
array.every((item) => condition)Examples
const ages = [15, 22, 31];
ages.some((a) => a < 18); // → true
ages.every((a) => a >= 18); // → falseconst cart = [{ title: "Tea", inStock: true }, { title: "Coffee", inStock: false }];
cart.some((item) => !item.inStock); // → true
cart.every((item) => item.inStock); // → false[].some((x) => x > 0); // → false
[].every((x) => x > 0); // → trueResults after // → aren't just written down — an automatic check runs and verifies them before every site update.
Why it works this way
some checks items in order and returns true at the first item for which the callback returned truthy — it doesn't even look at the rest. If none is found, the answer is false.
every is the reverse: it returns false at the first item that doesn't match, and true if they all do.
An empty array: some gives false (nothing matches), and every gives true (nobody breaks the condition). It's a mathematical convention, but it can surprise you — check empty lists separately if that matters to you.
Traps
How it differs from…
includes()includes looks for an exact value;
someuses any condition (an object's property, a range, a regular expression).array.find()find returns the matching item itself;
someonlytrueorfalse.