Understanding Equality Operators
Published
14 Feb 2023
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 trueHere, 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 truen 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 falseIn this case, the string "5" and the number 5 are not considered equal because they are of different data types. And similary
[] === false; // returns falseOperator 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.
Similar articles

Sorting Algorithms Explained
Bubble, selection, insertion, merge, quick, and heap sort: how each one works, where it shines, where it struggles, and animated demos that run the real algorithm code on the same starting data every time.
13 July 2026

Set Membership Testing: From Arrays to Bloom Filters
Every time Chrome warns you a site might be malicious, it just answered a membership question: is this URL in a set of bad ones? Here's how that check works, from a plain array to the probabilistic Bloom filter that makes it possible at scale.
10 July 2026

Why You Shouldn't Use Floating Point Math for Money in JavaScript
JavaScript numbers look like ordinary decimals, but they are stored as binary floating point. That is why 0.1 + 0.2 is not 0.3, and why checkout totals, tax lines, and ledger comparisons can quietly go wrong.
4 July 2026

RFC 10008: HTTP Finally Gets a Query Method
For thirty years, complex searches have been squeezed into GET query strings or smuggled through POST. RFC 10008 standardises QUERY: a safe, idempotent HTTP method with a request body. Here is why that matters and when you can actually use it.
3 July 2026
