Syntax, Parameter and Return Value
Syntax:
_.isEqual(value, other)
Parameters:
-
value: The value to compare.
- other: The other value to compare.
Return Value:
(boolean) - Returns true if the values are equivalent, else false.
Examples
Comparing Complex Objects:
JavaScript
var _ = require('lodash');
var object1 = { a: 1, b: { c: 2 } };
var object2 = { a: 1, b: { c: 2 } };
console.log(_.isEqual(object1, object2));

You can also try this code with Online Javascript Compiler
Run Code
Output:
true
Demonstrates the deep comparison of two objects with nested structures.
Checking Equality of Arrays:
JavaScript
var array1 = [1, [2, 3]];
var array2 = [1, [2, 3]];
console.log(_.isEqual(array1, array2));

You can also try this code with Online Javascript Compiler
Run Code
Output:
true
Shows deep comparison of arrays, including nested arrays.
Differentiating Between Similar-looking Values:
JavaScript
var obj1 = { a: 1, b: 2 };
var obj2 = { a: 1, b: '2' };
console.log(_.isEqual(obj1, obj2));

You can also try this code with Online Javascript Compiler
Run Code
Output:
false

You can also try this code with Online Javascript Compiler
Run Code
An example of how _.isEqual() can differentiate between values that look similar but have different types.
Use in Conditional Logic:
function updateData(newData, oldData) {
if (!_.isEqual(newData, oldData)) {
// Perform update
}
}

You can also try this code with Online Javascript Compiler
Run Code
Demonstrates using _.isEqual() in conditional logic to check if data has changed before performing an update.
Frequently Asked Questions
How does _.isEqual() handle circular references?
_.isEqual() can handle circular references in objects or arrays. It smartly tracks visited objects and arrays to avoid infinite loops in circular structures.
Is _.isEqual() efficient for large data structures?
While _.isEqual() is designed to be comprehensive, its deep comparison nature can be computationally intensive for very large or deeply nested structures. It should be used judiciously in performance-critical applications.
Can _.isEqual() compare function properties?
_.isEqual() compares function properties by reference, not by their internal structure or context. Two function properties are considered equal if they reference the same function.
Conclusion
Lodash's _.isEqual() method provides a robust solution for deep comparison of objects, arrays, and other complex data structures in JavaScript. It ensures accurate and comprehensive equality checks, far beyond what shallow comparison techniques can offer.
You can refer to our guided paths on the Coding Ninjas. You can check our course to learn more about DSA, DBMS, Competitive Programming, Python, Java, JavaScript, etc.
Also, check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.