Updated Nov 29, 2026

02 - Values and Equality

📋 Jump to Takeaways

🎁 0 == "" is true in JavaScript. So is null == undefined. Before you write a single if-statement, you need to know which equality operator to use and why.

Seven primitive types and one reference type. If you're coming from Go or Java, most of this maps cleanly. The surprises are in the details: two kinds of nothing, two kinds of equality, and a coercion system that will burn you if you let it.

Primitives vs Reference Types

Primitives are immutable. They're compared by value.

const a: string = "hello";
const b: number = 42;
const c: boolean = true;
const d: null = null;           // "no value" — you set this intentionally
const e: undefined = undefined; // "no value yet" — what uninitialized variables default to
const f: symbol = Symbol("id");
const g: bigint = 9007199254740993n;

// Compared by value
console.log("hello" === "hello"); // true
console.log(42 === 42);           // true

Reference types (objects, arrays, functions) are compared by reference, not by contents.

// Objects
const x = { name: "alice" };
const y = { name: "alice" };
console.log(x === y); // false — same contents, different references

const z = x;
console.log(x === z); // true — same reference

// Arrays
const a = [1, 2, 3];
const b = [1, 2, 3];
console.log(a === b); // false

// Functions
const fn1 = () => 42;
const fn2 = () => 42;
console.log(fn1 === fn2); // false

Same as Go's pointer comparison for structs. If you want deep equality, you need a library or write your own check.

The Two Nothings: null vs undefined

Go has nil. Python has None. TypeScript has two: null and undefined. They mean different things.

undefined means "no value was assigned." You get it automatically.

let x: string | undefined;
console.log(x); // undefined

function greet(name?: string): void {
  console.log(name); // undefined if not passed
}
greet();

const obj = { a: 1 };
console.log(obj["b"]); // undefined (key doesn't exist)

null means "explicitly set to nothing." You assign it on purpose.

const user: { name: string; deletedAt: Date | null } = {
  name: "alice",
  deletedAt: null, // intentionally empty
};

Use null when you want to say "this value is intentionally absent." Use undefined for "this was never set." Most APIs use undefined for optional values and null for explicit emptiness.

console.log(typeof null);      // "object" (yes, this is a bug from 1995)
console.log(typeof undefined); // "undefined"

== vs === (Loose vs Strict Equality)

strict equality (===) compares value and type. No surprises.

console.log(1 === 1);     // true
console.log(1 === "1");   // false (different types)
console.log(null === undefined); // false

== coerces types before comparing. This is where things get weird.

console.log(1 == "1");          // true  (string coerced to number)
console.log(0 == false);        // true  (boolean coerced to number)
console.log("" == false);       // true  (both coerce to 0)
console.log(null == undefined); // true  (special case)
console.log(null == 0);         // false (null only == undefined)

The coercion rules:

Expression Result Why
1 == "1" true string converts to number
0 == false true boolean converts to number
"" == false true both become 0
null == undefined true spec says so
null == 0 false null only coerces to undefined
NaN == NaN false NaN is not equal to anything

Always use ===. The only exception is value == null, which checks for both null and undefined in one shot. Some codebases allow this shorthand.

Truthy and Falsy

Every value is either truthy or falsy. if, &&, ||, and ! all use truthiness, not strict boolean checks.

Six falsy values. Everything else is truthy.

// All falsy
if (!false)     console.log("false is falsy");     // prints
if (!0)         console.log("0 is falsy");          // prints
if (!"")        console.log("empty string falsy");  // prints
if (!null)      console.log("null is falsy");       // prints
if (!undefined) console.log("undefined is falsy");  // prints
if (!NaN)       console.log("NaN is falsy");        // prints

// These are truthy (surprise!)
if ([])  console.log("empty array is truthy");  // prints
if ({})  console.log("empty object is truthy"); // prints
if ("0") console.log("string '0' is truthy");   // prints

In Go, if only accepts bool. if 0 is a compile error. In JS, if (0) is valid and evaluates to false. Takes some getting used to.

Optional Chaining (?.) and Nullish Coalescing (??)

Optional chaining short-circuits to undefined if any part of the chain is null or undefined.

interface User {
  name: string;
  address?: {
    city?: string;
  };
}

const user: User = { name: "alice" };

console.log(user.address?.city);           // undefined (no crash)
console.log(user.address?.city?.length);   // undefined (no crash)

In Go, you'd check each level manually:

// Go equivalent
if user.Address != nil && user.Address.City != "" {
    fmt.Println(user.Address.City)
}

Nullish coalescing (??) provides a default when the value is null or undefined. Not when it's falsy.

const port = Number(process.env.PORT) || 3000;
console.log(port); // uses PORT if set and numeric, 3000 otherwise

// ?? vs ||
const count = 0;
console.log(count || 10);  // 10 (0 is falsy, so || falls through)
console.log(count ?? 10);  // 0  (?? only triggers on null/undefined)

This distinction matters. Use ?? when 0, "", or false are valid values. Use || when you want to fall through on any falsy value.

Type Coercion Footguns

Type coercion in arithmetic and comparison. The rules are inconsistent and they will burn you.

// The + operator
console.log("5" + 3);    // "53" (number coerced to string, concatenation)
console.log("5" - 3);    // 2   (string coerced to number, subtraction)
console.log("5" * 2);    // 10  (string coerced to number)

// Array coercion
console.log([] + []);     // ""       (both become empty strings)
console.log([] + {});     // "[object Object]"
console.log({} + []);     // "[object Object]" (or 0 in some contexts)

// Boolean coercion
console.log(true + true); // 2 (booleans become 1)
console.log(true + "1");  // "true1" (boolean becomes string)

// The classic
console.log([] == false);  // true
console.log([] == ![]);    // true (yes, really)

Why [] == ![] is true:

  1. ![] evaluates to false (because [] is truthy)
  2. [] == false triggers coercion
  3. [] becomes "", false becomes 0
  4. "" becomes 0
  5. 0 == 0 is true

You don't need to memorize these rules. Just use === and explicit conversions.

// Explicit is always better
const input = "42";
const num = Number(input);     // 42
const str = String(42);        // "42"
const bool = Boolean("");      // false

Key Takeaways

  • Seven primitives (compared by value) and reference types (compared by reference)
  • undefined means "not assigned." null means "intentionally empty." Use both, know the difference.
  • Always use ===. The only acceptable == is value == null to check both null and undefined.
  • Six falsy values: false, 0, "", null, undefined, NaN. Everything else is truthy, including [] and {}.
  • Use ?? for defaults when 0 or "" are valid values. Use || when any falsy value should trigger the fallback.
  • Never rely on implicit type coercion. Use Number(), String(), Boolean() for explicit conversions.

🎁 In Go, a function is a function. In JavaScript there are three different ways to define one, and the choice changes how this behaves — which is the #1 source of bugs for backend developers crossing over.

📝 Ready to test your knowledge?

Answer the quiz below to mark this lesson complete.

Spot something off? Report an issue