Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Equality Comparisons  ›  Lesson

Strict Equality (===)

=== 12 minStrict equality is true only when both sides have the same type and value, compares objects by identity, treats NaN as unequal to itself and 0 as equal to -0.

Strict Equality (===)

In short: === never converts. Different types are always unequal. Primitives of the same type compare by value. Objects — including arrays and functions — are equal only if they are the very same object, not merely equal-looking ones. Two number quirks round it out: NaN === NaN is false, and 0 === -0 is true. Because it never surprises you with a hidden conversion, === should be your everyday default.

Three gates: clicked === selected → gate 1 same type? → gate 2 same object? → false (clicked was built fresh from the server, a different object with identical fields); fix: compare a field instead — clicked.id === selected.id

Compare the same object to itself

When two names point at the exact same object, === is true. Build a second, separate object with identical fields, and it is false:

javascript

current === selected is true — same object, same reference. A freshly written { title: "Blue Hour" } is a different object with matching contents, so it fails. The third line compares the title strings instead of the objects, and strings are primitives, so === compares them by value: true.

Compare a clicked song to the current one

This is why comparing two objects for "do they represent the same thing" fails when you compare the objects directly, even with matching data — exactly the trap the drawing walks through: clicked arrives freshly built from server data, so it is never the same object as selected, no matter how identical their fields are. Comparing an identifying field instead fixes it, because that field is a primitive, and primitives compare by value:

javascript

The first line is false — two separate objects. The second is true — id is a number, and numbers compare by value.

Is this the selected song?Step 1 of 4
  1. The check

    if (clicked === selected), where both came from different places in the code.

Quick practice

quick drill

What is "5" === 5?

quick drill

What is NaN === NaN?

quick drill

What is 0 === -0?

quick drill

What is [1] === [1]?

quick drill

What is a === b after const a = { plays: 1 }; const b = a;?

🎯 Your turn

A listener clicks a song and clickedSong === currentSong is false, even though both print { title: "Blue Hour", seconds: 214 }. Explain why and how to fix the comparison.

Write your explanation and submit it — there's no code to run here, just your reasoning.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainWhy the check failed

A listener clicks a song and clickedSong === currentSong is false, even though both print { title: "Blue Hour", seconds: 214 }. Explain why and how to fix the comparison.