Skip to content
javascript questions

JavaScript · 5 of 10junior~1 min

What is the difference between == and ===?

#core

== vs ===

=== (strict equality)

Compares value and type - no conversion.

js
1 === 1 // true 1 === '1' // false - different types null === null // true null === undefined // false

== (loose equality)

Compares values after type coercion - JavaScript converts types before comparing.

js
1 == '1' // true - '1' converted to 1 0 == false // true - false converted to 0 null == undefined // true - special rule null == 0 // false - special rule [] == false // true - both convert to 0 '' == false // true - both convert to 0

The coercion rules are complex

js
[] == ![] // true - this is why == is confusing // [] -> 0, ![] -> false -> 0

Object comparison

js
// both == and === compare references for objects const a = { x: 1 }; const b = { x: 1 }; a == b // false a === b // false a === a // true

Always use ===. The loose equality rules are confusing, hard to remember, and a common source of bugs. The only exception some teams make is x == null to check for both null and undefined at once.

updated

did you know it?