== vs === in JavaScript: What’s the Difference and When to Use Them

What's the real difference between == and === in JavaScript? This quick guide breaks it down and shows you when to use each — and why sticking to === is usually the smarter move.

== vs === in JavaScript: What’s the Difference and When to Use Them

If you’re writing JavaScript, you’ve probably seen both == and === used to compare values. But what’s the difference?

== (Loose Equality)

The == operator checks for equality of value, but it allows type coercion. That means JavaScript will try to convert the values to the same type before comparing.

'5' == 5 // true
true == 1 // true
null == undefined // true

It might seem helpful, but this behavior can lead to unexpected bugs.


=== (Strict Equality)

The === operator checks for equality of value and type. No conversions happen.

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

This makes comparisons more predictable and safer.


✅ Best Practice

Always use === unless you specifically need type coercion (which is rare and should be intentional).

Using === makes your code easier to understand and helps prevent hidden bugs.


In Short

Expression Result
'5' == 5 true
'5' === 5 false
null == undefined true
null === undefined false

Stick with === for clean, reliable code.