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

Animated Gradient Borders: A Better Loading State for AI Interfaces
Spinners and skeleton screens assume a request either hasn't returned yet or has. AI responses don't work that way, they stream in over seconds while you're already reading them. A rotating gradient border solves the "still working" signal without covering up content the user is actively looking at.
31 July 2026

Pathfinding Algorithms Explained
A live stealth pathfinding demo, and the three buckets every algorithm in the dropdown falls into: Naive, Uninformed, and Informed. Deep dives live under /algorithms.
26 July 2026

Tokens, Vectors, and Attention: How an LLM Actually Reads Your Prompt
An LLM never sees your words. It sees numbers: token IDs, vectors, and probabilities. Here's what actually happens between you hitting enter and the model's reply appearing, one step at a time.
26 July 2026

SOLID Principles Matter More With AI Coding Agents, Not Less
SOLID has been sitting in OOP textbooks for twenty years, half-forgotten by JavaScript developers who never wrote much Java. Hand a codebase to an AI agent and those five old rules turn into the difference between a codebase that keeps shipping and one that quietly rots.
25 July 2026
