Lodash _.isEqual() method
What you’ll learn
- How
_.isEqual(value, other)compares nested objects and arrays by content. - Why it differs from
===for objects with matching structure. - How dates and maps behave in deep equality checks.
- When reference identity still matters for functions and DOM nodes.
Prerequisites
Basic JavaScript object/array references and familiarity with strict equality (===).
- You know two object literals can look the same but still be different references.
- Try-it labs use lodash from the CDN.
Overview
Use _.isEqual when comparing cache snapshots, form drafts, API payloads, or test fixtures where nested structure matters more than object identity.
Deep structure
Compares nested values recursively rather than only top-level references.
Broad type support
Handles arrays, dates, maps, sets, typed arrays, and plain objects.
Identity exceptions
Functions and DOM nodes still compare by strict identity.
Syntax
_.isEqual(value, other) - value: first value to compare.
- other: second value to compare.
- Returns:
trueif deeply equivalent; otherwisefalse.
Nested objects with matching content
Different references can still be deeply equal when keys and values match recursively.
import isEqual from "lodash/isEqual";
var a = { user: { id: 7, roles: ["admin", "editor"] } };
var b = { user: { id: 7, roles: ["admin", "editor"] } };
console.log(
"deepMatch: " + isEqual(a, b) + "\n" + // true
"strictEq: " + (a === b) // false
); Arrays and Date values
Order matters in arrays; dates compare by their underlying time value.
import isEqual from "lodash/isEqual";
console.log(
"arrayOrder: " + isEqual([1, 2], [2, 1]) + "\n" + // false
"sameDateVal: " + isEqual(new Date("2024-06-01"), new Date("2024-06-01")) // true
); Map contents and function identity
Maps with equivalent entries can match deeply, while different function references do not.
import isEqual from "lodash/isEqual";
var mapA = new Map([["k", { n: 1 }]]);
var mapB = new Map([["k", { n: 1 }]]);
var fnA = function () { return 1; };
var fnB = function () { return 1; };
console.log(
"mapMatch: " + isEqual(mapA, mapB) + "\n" + // true
"fnIdentity: " + isEqual(fnA, fnB) // false
); 📋 _.isEqual vs related checks
| API | Matches |
|---|---|
_.isEqual(a, b) | Deep structural equality across many value types. |
a === b | Strict identity (or primitive equality) only. |
JSON.stringify(a) === JSON.stringify(b) | String-based approximation, sensitive to key order and unsupported values. |
_.isMatch(a, b) | Partial deep match, not full equivalence. |
Pitfalls to avoid
Deep checks in hot loops
Repeated deep comparisons can be expensive; cache normalized forms or narrow fields first.
Same body, different reference
Two separately created functions are not equal unless they are the exact same reference.
Need custom comparison rules?
Use _.isEqualWith when domain-specific tolerance (e.g., case-insensitive strings) is required.
❓ FAQ
Summary
- Purpose: compare complex values deeply when reference equality is too strict.
- Remember: functions and DOM nodes compare by identity, not by structure.
- Next: explore more on Lodash _.isEqualWith().
_.isEqual performs deep value comparison (arrays, objects, maps, sets, dates, typed arrays, and more), but functions and DOM nodes are compared by strict identity (===).
6 people found this page helpful
