Chapter III · Data Types

Null

Null is a special primitive value representing the intentional absence of any object reference.

01 What is Null?

The Null type has exactly one value: null. Unlike undefined (which signifies an accidental lack of value or uninitialized state), null is an intentional marker.

Developers use null to explicitly signal that a variable or object property should currently point to nothing.

02 Simple Example

Assigning null to clear an active object reference.

null.js
let currentUser = { name: "Alice" };
// User logs out, clearing reference
currentUser = null;
console.log(currentUser); // null

Setting currentUser to null indicates that the session has ended and no user object is currently loaded.

03 How It Works

A famous historical quirk in JavaScript causes typeof null to return "object". Despite this legacy bug in type tagging, null is strictly a primitive value.

Variable Reference Primitive Value

Null acts as a deliberate sentinel value pointing to an intentional absence of objects.

04 Step-by-Step

  1. A developer assigns null to a variable to clear its reference.

  2. The previous object reference becomes eligible for garbage collection if no other pointers remain.

  3. The variable holds the explicit empty sentinel value.

05 Mental Model

"An empty cardboard box left on the shelf with a label reading 'Empty on Purpose.'"

It's not missing its contents by accident; you purposefully cleared it out.

06 Interview Questions

Q: What's the practical difference between null and undefined?

undefined generally means 'not yet assigned' (the engine's default); null means 'intentionally empty,' set explicitly by the programmer.

Q: Why is typeof null 'object'?

A long-standing bug preserved for backward compatibility — it doesn't reflect null being an actual object internally.

Key Takeaway

Null is the intentional primitive value representing the deliberate absence of an object reference.

Related Concepts