Skip to the workspace
Pebellslearn by building
Equality Comparisons  ›  Lesson

Loose Equality (==)

== 12 min

javascript / Loose Equality (==)

Explore each scene at your own pace.

01 · Loose Equality (==)

Loose Equality (==)

Loose Equality (==)

Loose equality (==) can convert operands of different types before comparing them. It is not simply strict equality with a convenience feature.

Unexpected equality often comes from a conversion you did not make visible.

See the mechanism

Walk through the modelLoose Equality (==)Authored snapshots of the example; use Run below to test the code.
"4" == 4true
The comparison can convert unlike operand types.
1 of 3

Follow one example

javascript

These results are true, true, true and false. null and undefined have a special loose-equality relationship; null is not loosely equal to every numeric value that another operation might convert it into. Prefer === for ordinary checks and explicitly convert validated input where required.

Predict, change and check

Predict: Can you infer null == 0 from Number(null) being 0? Which rules does each operation use?

Test your explanation: Run Number(null) alongside null == 0. Explain why conversion in one operation is not a universal replacement rule.

The tasks below ask you to explain that result and apply the idea to different inputs.

Apply and distinguish

A rule with cases, not a numeric shortcut

The loose comparison algorithm first considers operand types and has special branches. null == undefined is true, but null == 0 is false even though Number(null) is zero. Do not substitute one conversion operation into a different comparison operation and assume the result is preserved.

If a product deliberately accepts both string and numeric identifiers, normalize that boundary under a documented schema instead of applying == everywhere. Objects can invoke conversion behavior during loose comparison. Strict equality is usually easier to reason about when both operands are already expected to have the same type.

Check your understanding: State the input contract, identify the operation that changes the result, and give one boundary input that would distinguish the correct rule from the mistake described above.

Scene 1 of 2
Project context and further resources
Additional resources
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.