What is the importance of _isEqual() method in JavaScript?


_isEqual() 

_isEqual() is from the underscore and lodash library of javascript. It is used to compare javascript objects. The importance of this method is that it doesn't care about the order of properties while comparing the objects. It only checks whether the properties in the two objects are equal or not. Whereas JSON.stringify(), which is used in comparing the objects, checks even the order of properties of the objects, making _isEqual() the better option. 

syntax

_.isEqual(object1, object2);

It accepts two objects as parameters and scrutinizes whether they are equal or not.

Example

Live Demo

<html>
<head>
<script src =
   "https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js">
</script>
</head>
<body>
<script>
   var obj1 = {name: "Sikha", designation: "Content developer"};
   var obj2 = {name: "Sikha", designation: "Content developer"};
   document.write(_.isEqual(obj1, obj2));
</script>
</body>
</html>

Output

true


In the following example, Both JSON.stringify() and _isEqual() methods are used. Since the order of properties is not a problem for the _isEqual() method it gives out true as output whereas JSON.stringify() gives out false as output.

Example

Live Demo

<html>
<head>
<script src =
   "https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js">
</script>
</head>
<body>
<script>
   var obj1 = {name: "Sikha", designation: "Content developer"};
   var obj2 = {designation: "Content developer", name: "Sikha"};
   document.write(_.isEqual(obj1, obj2));
   document.write("</br>");
   document.write(JSON.stringify(obj1) === JSON.stringify(obj2));
</script>
</body>
</html>

Output

true
false

Updated on: 30-Jul-2019

952 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements