Strict vs Loose Equality
You'll learn the fundamental difference between == (loose equality with coercion) and === (strict equality without coercion).
JavaScript gives you two ways to check if values are equal, and understanding when each applies type coercion is crucial for writing predictable code.
The Core Difference
Loose equality (==) compares values after attempting to convert them to the same type. If you compare a string to a number, JavaScript coerces one to match the other before checking equality.
Strict equality (===) compares both the value and the type without any coercion. If the types don't match, it immediately returns false—no conversion happens.
Think of == as a lenient friend who says "close enough!" while === is precise and says "must be exactly the same."
Examples
5 == "5" // true (string "5" coerced to number 5)
5 === "5" // false (different types: number vs string)
0 == false // true (false coerced to 0)
0 === false // false (different types: number vs boolean)
null == undefined // true (special case in loose equality)
null === undefined // false (different types)
Why It Matters
Loose equality follows complex coercion rules you've learned (ToNumber, ToString, ToPrimitive). This can lead to unexpected results. Strict equality is straightforward: same type and same value, or it's not equal.
Key Takeaway: Use === (strict equality) by default to avoid unexpected type coercion. Only use == (loose equality) when you specifically need or understand the coercion behavior—such as checking for null or undefined together.