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

What Australian CTOs Should Put in the AI Governance Pack
The board is asking. The regulator is watching. The CTO is the one who has to translate "AI governance" from a policy concept into a document set that actually governs something. Here is what a credible AI governance pack looks like for an Australian organisation in 2026 — and what each section needs to do to survive scrutiny.
21 Sept 2026

Governing Agentic AI: Human-in-the-Loop Patterns When Tools Can Act (MCP Edition)
Agentic AI doesn't just recommend — it acts. That changes governance from a policy question into an engineering question: which patterns actually limit blast radius, satisfy a human reviewer, and survive scrutiny from a regulator or court? A practical playbook for MCP-connected agents, with a checklist for regulated industries.
16 Sept 2026

Vercel Can Do WebSockets Now. Here's Where a Chat Room Still Breaks It.
Vercel shipped native WebSocket support in 2026, so a Function really can hold a socket open. But the connection is still a function invocation with a duration ceiling and no fan-out. Here's where that's fine for a chat app, where it isn't, and how Supabase Realtime covers the gap.
15 Aug 2026

AI-First Programming Languages: What They Are, and Whether You Should Use One Yet
In the last year a genuinely new category of programming language appeared: ones where the AI, not you, is the intended author. Here's what the categories are, which projects actually have traction, and which of them I'd be willing to put in production today.
9 Aug 2026
