Recent chats
ASKGEEZ Q&A GUIDETechnology & devices

How does JavaScript compare objects with `===`?

Quick answer

For objects, === checks whether both sides refer to the same object, not whether their properties match. So two separately created objects with identical contents compare as false, while two variables pointing to one object compare as true. For primitive values, strict equality compares values without converting types; for example, 3 === 3 is true, but 3 === "3" is false. [S1]

LET’S WORK THROUGH IT

The question, the details, the next step.

Question

I have two JavaScript objects that look identical, but first === second is false. Why? I expected the matching properties to make them equal.

Geez

That result is expected. With objects, === asks whether the two operands refer to the very same object. It does not compare their properties one by one. Separately creating two objects with the same property names and values gives you two distinct objects, so strict equality is false. [S1]

For example:

const first = { name: "Mina", score: 8 };
const second = { name: "Mina", score: 8 };

console.log(first === second); // false

The contents match, but first and second refer to separately created objects. Think of === here as an identity check, not a test for matching contents. If you need to decide whether two objects are equivalent for your application, define what “equivalent” means and compare the relevant data accordingly.

Follow-up question

What changes if I assign one object to a second variable instead of writing the same object literal twice?

Geez

Both variables then refer to the same object, so comparing them with === returns true. A runnable example that shows both cases is:

const first = { name: "Mina", score: 8 };
const separate = { name: "Mina", score: 8 };
const shared = first;

console.log(first === separate); // false
console.log(first === shared);   // true

The first comparison is false because separate was created as another object. The second is true because shared was assigned the object already referred to by first. The example demonstrates reference identity; it does not test whether two separately created objects have equivalent data. [S1]

Follow-up question

Is === different for strings and numbers? I’m also unsure whether it converts one side to match the other.

Geez

Yes. For primitive values of the same type, strict equality compares their values. It does not coerce one type to another, so 3 === 3 is true, but 3 === "3" is false. Objects follow the identity rule instead. [S1]

console.log(3 === 3);       // true
console.log(3 === "3");    // false
console.log({ id: 1 } === { id: 1 }); // false

One technical boundary: the general primitive rule has numeric edge cases. NaN is unequal to itself, while positive and negative zero compare equal. [S1] These examples focus on ordinary numbers, strings, and object references; they are not a deep comparison utility.

Follow-up question

What should I check when this comparison is in my own code?

Geez

Start by identifying what the comparison is meant to answer. If you want to know whether two variables refer to one object, === is appropriate. If you want to know whether their contents match, decide which properties matter and compare those deliberately; the result you want cannot be inferred from === alone.

A useful way to inspect a case is to write down where each value comes from, then log the comparison and the values separately. For example, label the values as “created separately” or “assigned from the same variable.” This helps distinguish a reference-identity check from a content comparison without assuming that identical-looking objects are the same object.

A practical checklist

  • Identify whether each operand is an object or a primitive value.

  • For objects, check whether both variables refer to the same object or to separately created objects.

  • Use === when the question is whether object references are identical.

  • If matching contents are the goal, decide which properties count and compare those explicitly.

  • For primitive comparisons, check that the values have the same type and consider numeric edge cases such as NaN.

A few more useful answers

Does `===` compare every property of two objects?

No. For objects, it checks whether both operands refer to the same object, not whether their properties match. [S1]

Why is `3 === "3"` false?

Strict equality does not convert values to make their types match. The number 3 and the string "3" are different types, so this comparison is false. [S1]

Can `x === x` be false?

Yes, if x is NaN: NaN is unequal to itself under strict equality. Positive and negative zero, by contrast, compare equal. [S1]

Sources

References used for this guide.

  1. [S1] Equality comparisons and sameness — JavaScript | MDN
    developer.mozilla.org · Checked

YOUR QUESTION. YOUR DETAILS.

Let’s work through your situation.

Get a tailored answer or help with your next task. Starts at $1, with a quote before you pay.

Ask your own question
← Back to Geez at Work