Lodash _.every() method
What you’ll learn
- How
_.every(collection, predicate)validates a rule across an entire collection. - Short-circuit behavior and why side effects inside predicates are risky.
- How Lodash compares to native
Array.prototype.everyand to_.some. - Try each example in the editor (
?tryit=1,2,3) with Lodash from a CDN.
Prerequisites
Optional warm-up: _.countBy() for grouping patterns before validation-style iteration.
- You can write predicates that return strict booleans.
- You can open Try-it labs or run snippets locally.
Overview
_.every encodes universal checks—permissions matrices, batch validators, “all rows qualify” guards—while exiting early the moment one element breaks the rule.
Short-circuits
Failed checks stop traversal—useful when predicates are expensive or collections are large.
Iteratee sugar
Reuse Lodash iteratee styles—functions, paths, or partial object constraints.
Arrays or objects
The same helper walks indexed lists or keyed plain objects consistently.
Syntax
_.every(collection, predicate) - collection: array, array-like, or plain object Lodash can iterate.
- predicate: invoked as
(value, index|key, collection); falsy results fail the check. - Returns: boolean—
trueonly when every invocation is truthy (including empty collections).
Numeric guard with a function predicate
Confirm every reading stays inside an acceptable band before aggregating metrics.
import every from "lodash/every";
every([62, 70, 68], (temp) => temp >= 60 && temp <= 72);
// → true Partial object match iteratee
Lodash compares each element against the template object—handy for uniform flag checks across rows.
import every from "lodash/every";
every(
[
{ id: 1, role: "editor", active: true },
{ id: 2, role: "viewer", active: true }
],
{ active: true }
);
// → true Empty collections and vacuous truth
No elements means no counterexamples—both Lodash and native every resolve to true. Validate length first when emptiness should fail your domain rule.
import every from "lodash/every";
every([], () => false);
// → true (predicate never runs) 📋 _.every vs some, native every
| API | Outcome | Best when |
|---|---|---|
_.every(collection, predicate) | All elements pass | Universal validation across arrays or objects |
_.some(collection, predicate) | At least one passes | Existential checks or early wins |
array.every(predicate) | All elements pass | Pure arrays without Lodash iteratee sugar |
Pitfalls to avoid
Vacuous passes
Guard minimum cardinality yourself when “must have at least one row” is part of the business rule.
Side effects in predicates
Short-circuiting means later elements might never run—keep predicates pure or explicitly document ordering assumptions.
Truthy versus strict true
Lodash treats predicate results with ordinary truthiness rules—return explicit booleans when ambiguity hurts readability.
❓ FAQ
Summary
- Purpose:
_.every(collection, predicate)returns whether every element satisfies the predicate. - Contrast: flip to
_.somefor existential checks; remember empty collections yieldtrue. - Next: Lodash _.filter(), Lodash _.countBy() (previous), or collection hub.
For an empty collection, Lodash _.every returns true—the same vacuous-truth behavior as Array.prototype.every. Pair that rule with _.some when you need “at least one” instead.
6 people found this page helpful
