Bugs
JavaScript == vs ===: Why Loose Equality Causes Bugs
JavaScript's == performs type coercion before comparison, producing results that seem counterintuitive and cause hard-to-find bugs.
❌ Surprising Results from ==
console.log(0 == false); // true
console.log('' == false); // true
console.log(null == undefined); // true
// Dangerous:
if (user.role == 0) { // Also matches '', false, null!
grantAdminAccess();
}✅ Safe with Strict Equality
console.log(0 === false); // false
console.log('' === false); // false
// Safe:
if (user.role === 0) { // Only matches the number 0
grantAdminAccess();
}💡
Pro tip: Enable ESLint's eqeqeq rule to enforce === across your entire codebase. The only accepted use of == is value == null to check both null and undefined.
Paste this code into LearnCodeGuide
Detect JavaScript vulnerabilities and bugs automatically with AI-powered analysis.
Analyze JavaScript Code →