Course contentsShow
← JavaScript
Lesson 204 of 2,7236. Booleans and ConditionalsFree lesson

Equality: == vs ===

Learn the difference between loose equality (==) and strict equality (===) and why === is preferred.

Equality: == vs ===

You'll learn: The crucial difference between JavaScript's two equality operators and why one is almost always the better choice.

Two Ways to Compare

JavaScript gives you two operators to check if values are equal:

  • == (loose equality) – compares values after attempting type conversion
  • === (strict equality) – compares both value AND type, with no conversion

Think of == as a lenient teacher who accepts "close enough" answers, while === is strict and demands exact matches.

How They Differ

With ==, JavaScript tries to be "helpful" by converting types before comparing:

5 == "5"     // true (string converted to number)
0 == false   // true (false converted to 0)
"" == false  // true (both become falsy/0)
null == undefined  // true (special case)

With ===, no conversion happens—both type and value must match:

5 === "5"    // false (number vs string)
0 === false  // false (number vs boolean)
"" === false // false (string vs boolean)
null === undefined  // false (different types)

Why === Is Preferred

The type conversions == performs follow complex rules that can lead to surprising bugs. Using === makes your code's intentions crystal clear: you're checking for true equality, not "close enough."

Most experienced JavaScript developers use === by default and only reach for == in rare, specific situations (like checking if a value is null or undefined simultaneously).

Key Takeaway: Always use === (strict equality) unless you have a specific reason to allow type conversion—it prevents unexpected behavior and makes your code more predictable.