← All posts

== vs ===: The JavaScript Truth Showdown

Learn the difference between == and === so you don’t get caught by JavaScript’s type juggling traps.

🚀 The Simple Version

What’s the Big Deal?

In JavaScript, == checks if two values are equal after doing a bit of “type magic” (coercion). === checks if they’re equal and of the same type. The difference is like asking a waiter if you want a coffee or a tea, but the waiter keeps mixing up the cups.

Type Coercion 101

When you use ==, JavaScript will try to convert the values to a common type before comparing. That can lead to surprising results:

0 == "0"      // true – string becomes number
false == 0    // true – false becomes 0
null == undefined // true – special case

Why Beginners Get Confused

Most new coders come from languages that never change types for you. They write something like:

if (a == 5) { … }

and expect it to be safe, but if a is actually "5" (a string), it still passes. That can hide bugs that only show up in production.

When to Use ===

  • When you want a strict, predictable comparison.
  • When you’re checking numbers, strings, booleans, objects, etc.
  • When you’re writing tests or assertions.

In almost every case, === is the safer choice. Use == only when you really need the conversion magic and you know the values.

Quick Cheat Sheet

• == – loose equality, type conversion. • === – strict equality, no conversion. • Prefer === unless you have a specific reason to use ==.