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

=== is strict equality types must match. == is loose equality JavaScript coerces types before comparing, which causes surprises.

JavaScript

Overview

`===` (strict equality) compares both value and type. `==` (loose equality) first coerces both sides to the same type, then compares this leads to notoriously confusing results. Always use `===` in production code.

Key Points

  • •`===` returns `true` only when both value AND type are the same.
  • •`==` converts types before comparing (type coercion) can produce unexpected `true`.
  • •Notable `==` surprises: `0 == false` → true, `"" == false` → true, `null == undefined` → true.
  • •The only legitimate use of `==` is `value == null`, which is true for both `null` and `undefined`.
  • •ESLint rule `eqeqeq` enforces `===` everywhere.

Examples

=== vs == in practice

javascript
// === (strict always predictable)
1 === 1       // true
1 === '1'     // false different types
null === undefined // false

// == (loose type coercion surprises)
1 == '1'      // true  (string coerced to number)
0 == false    // true  (false coerced to 0)
'' == false   // true
null == undefined // true (special rule)
null == 0     // false (null only equals undefined with ==)

The null check idiom the one valid use of ==

javascript
// Checks for both null AND undefined in one expression
if (value == null) {
  // value is null or undefined
}

// Equivalent verbose version
if (value === null || value === undefined) { }

Pro Tip

Use `===` everywhere except the `== null` pattern. If a linter warns about `==`, the only exception to disable is for that null check pattern.