DEV Community

Cover image for Unraveling the Enigma: Exploring Object Equality in JavaScript and Beyond
Ashraf Mehdaly
Ashraf Mehdaly

Posted on

Unraveling the Enigma: Exploring Object Equality in JavaScript and Beyond

Introduction:
JavaScript is a widely used programming language known for its flexibility and dynamic nature. However, when it comes to object comparison, JavaScript can behave in unexpected ways. One peculiar aspect is that two seemingly similar objects are not considered equal when using the equality operator (==) or strict equality operator (===). This essay explores the reasons behind this behavior and compares it with other programming languages like C#.

1. Reference Equality in JavaScript:
In JavaScript, objects are reference types, and when comparing objects using the equality operator (==) or the strict equality operator (===), the comparison is based on their references rather than their contents. Two objects with identical key-value pairs are considered different if they occupy different memory locations. Let's illustrate this with a code example:

const obj1 = { name: "John", age: 30 };
const obj2 = { name: "John", age: 30 };

console.log(obj1 == obj2); // Output: false
console.log(obj1 === obj2); // Output: false
Enter fullscreen mode Exit fullscreen mode

In this example, obj1 and obj2 have the same key-value pairs, but they are two distinct objects with different references. Hence, the comparison yields false.

2. Value Equality in JavaScript:
If you want to compare the contents of two objects rather than their references, you need to implement a custom comparison function. One way to achieve this is by converting the objects to JSON strings and then comparing those strings. However, this approach has limitations, especially when dealing with nested objects or functions.

function areObjectsEqual(obj1, obj2) {
  return JSON.stringify(obj1) === JSON.stringify(obj2);
}

const obj3 = { name: "John", age: 30 };
const obj4 = { name: "John", age: 30 };

console.log(areObjectsEqual(obj3, obj4)); // Output: true
Enter fullscreen mode Exit fullscreen mode

3. Object Comparison in C#:
C# is a statically typed language, and its object comparison behavior is different from JavaScript. By default, object comparisons in C# are reference-based, similar to JavaScript's strict equality (===). However, C# allows you to override the Equals method to customize object comparison based on their contents.

class Person
{
    public string Name { get; set; }
    public int Age { get; set; }

    public override bool Equals(object obj)
    {
        if (obj == null || !(obj is Person))
            return false;

        var otherPerson = (Person)obj;
        return this.Name == otherPerson.Name && this.Age == otherPerson.Age;
    }
}

Person person1 = new Person { Name = "John", Age = 30 };
Person person2 = new Person { Name = "John", Age = 30 };

Console.WriteLine(person1 == person2); // Output: False
Console.WriteLine(person1.Equals(person2)); // Output: True
Enter fullscreen mode Exit fullscreen mode

In this example, we override the Equals method in the Person class to compare the objects based on their Name and Age properties.

Conclusion:
In JavaScript, two similar objects are not considered equal when using the equality or strict equality operators because the comparison is based on their references, not their contents. To compare object contents in JavaScript, you need to implement a custom comparison function. On the other hand, C# allows developers to override the Equals method to customize object comparisons based on their properties. Understanding these differences is crucial for writing robust code when dealing with objects in JavaScript and other programming languages like C#.

Top comments (0)