Chapter IV · Operators & Expressions
Strict Equality
The === operator compares two values for equality without any type conversion — if the types differ, the result is immediately false, no exceptions.
01 What Is Strict Equality?
=== answers a simpler question than ==: "are these the same type AND the same value?" No coercion, no spec-defined conversion table to memorize — which is exactly why it's the recommended default for comparisons in JavaScript.
02 Simple Example
Strict equality refusing to coerce types.
console.log(5 === '5'); // false — different types
console.log(5 === 5); // true
console.log(null === undefined); // false — strict about type, unlike ==
03 How It Works
The engine first checks whether the two operands share the same type. If not, it returns false immediately — no conversion happens. If the types match, it compares the actual values directly (with one well-known quirk: NaN === NaN is false, because NaN is defined as never equal to anything, including itself).
04 Production Example
Checking a specific status value — the kind of comparison that appears constantly in real applications.
function isOrderShipped(orderStatus) {
return orderStatus === 'shipped';
}
// Checking for NaN correctly, since NaN === NaN is false:
function isValidQuantity(quantity) {
return !Number.isNaN(quantity) && quantity > 0;
}
05 Mental Model
"A strict border agent: same passport type AND same details, or you're turned away — no attempt to guess what you 'probably' meant."
06 Comparison: == vs ===
| == | === | |
|---|---|---|
| Type conversion | Yes | No |
| 5 vs '5' | true | false |
| Recommended default | No | Yes |
07 Interview Questions
Q: Why is NaN === NaN false?
NaN is spec-defined to never equal anything, including itself — use Number.isNaN() to actually test for it.
Q: Does === ever coerce types?
Never — if the operand types differ, it returns false immediately without any conversion.
Key Takeaway
=== compares type and value with no coercion, which makes results predictable — it's the default comparison operator in modern JavaScript.