When == Might Be Acceptable
What you'll learn: While === is the safer default, there's one scenario where == can actually simplify your code: checking for null or undefined together.
The Null/Undefined Check Pattern
You've learned that == performs type coercion, which usually creates unpredictable behavior. However, there's a deliberate exception built into JavaScript: null == undefined evaluates to true.
This quirk becomes useful when you want to check if a variable is "nullish" (either null or undefined) without caring which one it is:
let value = null;
// Using strict equality requires checking both:
if (value === null || value === undefined) {
console.log("Value is nullish");
}
// Using loose equality is shorter:
if (value == null) {
console.log("Value is nullish"); // Works for both null and undefined
}
Both null and undefined represent "no value," and you often want to treat them the same way. The == null pattern checks both at once.
Why This Works
JavaScript's coercion rules specify that null and undefined are only loosely equal to each other and nothing else. So value == null won't accidentally match 0, false, or empty strings—it only catches actual absence of value.
Still Use With Caution
Even this exception is debatable. Some developers prefer the explicit value === null || value === undefined for clarity. Others use modern techniques you'll learn later. The point: == isn't always evil, but this null check is its only defensible use case.
Key Takeaway: The one acceptable use of == is checking value == null to catch both null and undefined simultaneously, though even this remains controversial.