Why == and === behave differently in JavaScript

Hey friends! 👋

If you’ve ever compared two values and got an unexpected result, don’t worry, that happens lots of times with == and ===. JavaScript comparisons can be behave strangely until you know what’s happening behind the scenes.

I’m going to make this as simple as possible.

1️⃣ === → Strict equality

=== checks two things:

  1. Are the values the same?
  2. Are the types the same?

Both must match.

Examples:

5 === 5        // true
5 === "5"      // false
true === 1     // false
null === undefined // false

If the types don’t match, JavaScript won’t even try. It just says “nope”.

2️⃣ == → Loose equality

== tries to be “helpful” by converting values before comparing them.
This is called type coercion.

Examples:

5 == "5"       // true  (string → number)
true == 1      // true  (boolean → number)
false == 0     // true
null == undefined // true  (special case)

It looks helpful…
but this “helpfulness” is what leads to weird behaviour like:

"" == 0        // true
"0" == 0       // true
[] == 0        // true

You’re not meant to memorise these.
The rule is simple:

== converts. === doesn’t.

3️⃣ Why this happens

JavaScript tries to compare values with == by making them the same type first.

A few conversions that happen behind the scenes:

  • strings → numbers
  • booleans → numbers
  • arrays → strings
  • null and undefined → only equal to each other

It does all these steps automatically — which is why it gets confusing fast.

4️⃣ Which one should you use?

For beginners (and honestly, most cases), stick with:

===

It’s predictable.
It won’t surprise you.
And it forces you to know what type you’re comparing.

Use == only when you fully understand the conversion rules — or when you specifically want:

null == undefined

to be true.

5️⃣ A simple demo

Try this in your console:

console.log(0 == false);   // true
console.log(0 === false);  // false

console.log("5" == 5);     // true
console.log("5" === 5);    // false

You’ll instantly see the difference.

6️⃣ Quick summary

Comparison Checks Type? Converts Values? Example
=== Yes No "3" === 3 → false
== No Yes "3" == 3 → true

Short version:

=== is strict and honest.
== tries too hard.

🙋🏾‍♀️ Wrap-Up

If JavaScript comparisons have ever confused you, it’s not your fault.
The coercion rules behind == are messy.
Using === gives you cleaner logic and fewer surprises.

That’s it for today!
What should we talk about next Wednesday? Drop it below 👇

Connect with me on GitHub

Was this tutorial helpful? Got questions? Drop them in the 💬, I love feedback.

I’m keeping these light, fun, and useful, one small project at a time.
If you enjoyed this, leave a 💬 or 🧡 to let me know.

Follow me for more short, beginner-friendly JavaScript lessons every week.

I'm still hunting for full stack roles,
contract or full-time.
Please check out my [Portfolio](https://gift-egbonyi.onrender.com)

Portfolio

Web trails:
LinkedIn | X (Twitter)

See you next Wednesday 🚀, hopefully, on time!

Till then, write clean code and stay curious. 🦋

Similar Posts