JavaScript Loose Equality and Coercion Rules
Quick answer
Loose equality == can compare values after applying type-specific conversion rules. It is not a simple conversion of both operands to strings or Booleans.
How it works
Same-type operands use strict equality behavior. Null and undefined compare equal to each other. A Boolean becomes a number; a string compared with a Number can be converted to Number. Object-versus-primitive comparisons may invoke object-to-primitive conversion. BigInt has additional mixed-type rules; not every pair of types is converted. The browser legacy document.all exception is outside this lesson. Use explicit normalization and strict equality by default.
console.log(5 == "5");console.log(5 === "5");console.log(false == 0);Expected output in Node.js:
truefalsetrueThe loose cases use numeric conversion; strict equality does not.
console.log(null == undefined);console.log(null == 0);console.log("" == 0);Expected output in Node.js:
truefalsetrueNullish equality is special; the empty string converts to zero in numeric comparison.
Common mistakes
Do not assume loose equality is transitive across mixed types. Prefer an explicit domain contract instead of relying on accidental coercion.
Practice
Predict the second example, then compare each pair using strict equality and explain the differences.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
JavaScript Loose Equality and Coercion Rules — Knowledge Check
Apply the taught concept and reason about the result.
What is null == undefined?
What is null == 0?
What is false == "0"?
What is "" == 0?
Which policy makes mixed-input comparisons clearer?