What is every()?
every(predicate) returns true if the callback returns a truthy value for every element, and stops at the first element that fails; it returns true for an empty array.
Typical uses
- Validation:
fields.every(f => f.value.trim() !== "")to enable a submit button. - Permission checks:
requiredRoles.every(r => user.roles.includes(r)). - Type checks:
values.every(Number.isFinite). - "All selected?" checkbox logic.
Short-circuiting
It stops as soon as one element fails, so it is efficient even on large arrays when a failure appears early.
The empty array case
[].every(anything) is true. This is called vacuous truth: there is no element that fails. It is logically correct but can surprise you; for example "all items are paid" is true for an empty cart. Add an explicit length check when "empty" should mean "no".
every vs some
every: all must pass (logical AND).some: at least one must pass (logical OR).!list.some(x => bad(x))is the same aslist.every(x => !bad(x)).
TypeScript tip
every accepts a type predicate, so values.every((v): v is string => typeof v === "string") narrows values to string[] inside the if.
Code example
1const form = [2 { name: "email", value: "ada@example.com" },3 { name: "password", value: "hunter22" },4 { name: "nickname", value: " " },5];67const isComplete = form.every((f) => f.value.trim() !== "");8console.log(isComplete); // false (stops at "nickname")910const scores = [72, 88, 95];11console.log(scores.every((s) => s >= 70)); // true12console.log(["1", 2, 3].every((v) => typeof v === "number")); // false1314// Required permissions15const user = { roles: ["editor", "reviewer", "viewer"] };16const canPublish = ["editor", "reviewer"].every((r) => user.roles.includes(r));17console.log(canPublish); // true1819// Vacuous truth: empty arrays pass20const cart = [];21console.log(cart.every((item) => item.paid)); // true22console.log(cart.length > 0 && cart.every((item) => item.paid)); // false - usually what you mean2324// Short-circuit in action25let checked = 0;26[1, 2, -3, 4, 5].every((n) => (checked++, n > 0));27console.log("checked", checked, "items"); // checked 3 items
Practice this question
Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the JavaScript set.