Equality vs Identity: == and ===
What you'll learn: The critical difference between JavaScript's two comparison operators and why one can surprise you.
The Core Difference
JavaScript gives you two ways to check if values are "equal":
==(loose equality) compares values after attempting type conversion===(strict equality) compares both value and type without any conversion
Think of == as a lenient friend who says "close enough!" while === is precise and demands exact matches.
How Loose Equality Works
With ==, JavaScript tries to be "helpful" by converting types before comparing:
5 == "5" // true (string "5" converted to number)
true == 1 // true (boolean converted to number)
null == undefined // true (special rule)
0 == false // true (both become 0)
How Strict Equality Works
With ===, no conversions happen—both type and value must match:
5 === "5" // false (number vs string)
true === 1 // false (boolean vs number)
null === undefined // false (different types)
0 === false // false (number vs boolean)
5 === 5 // true (same type, same value)
When to Use Which
Almost always use ===. Loose equality's conversions create confusing bugs. The only common exception: checking if something is null or undefined together:
value == null // true if value is null OR undefined
Key Takeaway: Use === for predictable comparisons. The == operator performs type coercion that often leads to unexpected results.