JavaScript - Comparison Operators

Overview

Estimated time: 15–25 minutes

Compare values for equality and ordering. Prefer strict equality (===) and strict inequality (!==).

Learning Objectives

  • Use ===/!== to avoid coercion surprises.
  • Compare numbers and strings correctly.

Prerequisites

Examples

3 === 3        // true
3 == '3'       // true (coercion)
3 === '3'      // false (no coercion)
'a' < 'b'      // true (lexicographic)

Common Pitfalls

  • Using == with mixed types can coerce: '' == 0 is true.
  • NaN === NaN is false; use Number.isNaN.