Understanding Equality Operators

In JavaScript, equality comparisons are an important aspect of the language, and it is crucial to understand the difference between == and ===. While both operators are used to compare values, they behave differently, which can lead to unexpected results if you are not aware of these differences.

== Operator

The == operator performs type coercion before checking for equality, which means that it converts the values being compared to a common type before performing the comparison.

"32" == 32; // returns true

Here, the string "5" is automatically converted to the number 5 before being compared to the number 5. This type of comparison can be useful in some cases, but it can also lead to unexpected results if you are not aware of how type coercion works. For example:

[] == false; // returns true

n this case, the empty array is converted to the number 0 before being compared to false, which is converted to 0 as well.

=== Operator

The === operator performs strict equality comparison and does not perform type coercion. This means that it only returns true if the values being compared are of the same data type and have the same value. For example:

"32" === 32; // returns false

In this case, the string "5" and the number 5 are not considered equal because they are of different data types. And similary

[] === false; // returns false

Operator on Objects

In JavaScript, the == operator compares objects by reference, not by value. This means that when two variables refer to the same object in memory, the interpreter will consider them  equal.

let obj1 = {name: "John"};
let obj2 = {name: "John"};

if (obj1 == obj2) {
  console.log("These objects are equal");
} else {
  console.log("These objects are not equal");
}

In the above code example, the if statement will log "These objects are not equal" because obj1 and obj2 reference two different objects in memory, even though they have the same properties and values.

Its important to understand the difference as this type of behaviour can lead to difficult to identify bugs in your code.

If we look at the following code snippet.

let obj1 = {name: "John"};
let obj2 = obj1;

if (obj1 == obj2) {
  console.log("These objects are equal");
} else {
  console.log("These objects are not equal");
}

In the above example, the if statement will log "These objects are equal" because obj2 is assigned the reference to obj1, so both obj1 and obj2 are refusing to the same object in memory.

If you need to compare the values of two objects, you can write a function that checks the properties and values of each part of the objects. You can alternatively make use of  a library like lodash or underscore, which provides utility functions for comparing objects.

Conclusion

If you want to be a good developer, understanding the difference between == and === is essential for writing efficient and maintainable JavaScript code.

Its generally agreed that you should be use the === operator when doing comparisons as it provides a more predictable outcome, particularly in cases where the data type coercion could lead to inaccurate results. Using === helps to make sure that you are comparing values of the same data type, preventing difficulty to drug runtime issues.

You've successfully subscribed to Twisted Brackets
Great! Next, complete checkout to get full access to all premium content.
Error! Could not sign up. invalid link.
Welcome back! You've successfully signed in.
Error! Could not sign in. Please try again.
Success! Your account is fully activated, you now have access to all content.
Error! Stripe checkout failed.
Success! Your billing info is updated.
Error! Billing info update failed.