Course contentsShow
← JavaScript
Lesson 57 of 2,7232. Values and TypesFree lesson

Comparing Primitives

Explore how numbers, strings, booleans, null, undefined, and symbols are compared for equality.

Comparing Primitives

What you'll learn: How JavaScript determines if two primitive values are equal.

When you compare primitive values (numbers, strings, booleans, null, undefined, and symbols), JavaScript checks whether their actual values match—not whether they point to the same location in memory. This is fundamentally different from how objects are compared.

How It Works

With primitives, comparison is straightforward:

5 === 5                    // true (same number)
"hello" === "hello"        // true (same string)
true === true              // true (same boolean)
null === null              // true
undefined === undefined    // true

Even if two primitives are stored in different variables, they're equal if their values match:

let x = 42;
let y = 42;
x === y  // true — both hold the value 42

Special Cases

Symbols are unique, even if they look identical:

Symbol("id") === Symbol("id")  // false — each Symbol is unique

NaN is special—it never equals itself:

NaN === NaN  // false (use Number.isNaN() to check)

Why This Matters

Since primitives are immutable and copied by value, comparing them checks the actual content, not memory addresses. Two strings with the same characters are always equal, regardless of where they came from.

Think of primitives like comparing the messages on two sticky notes. If both say "hello," they're equal—it doesn't matter which notepad they came from.

Key Takeaway: Primitive values are compared by their actual content, making === check whether two primitives hold the exact same value.