Compile Ready
Module 2 · Variables & Data Types

null

Beginner6m read3m practice9m total
nullAbsenceEqualityJSON

Introduction

null is a primitive value that usually means intentional absence: a value is expected, but it is deliberately empty. It is different from undefined, which usually means missing, uninitialised, or not provided.

The classic quirk is typeof null === 'object', but null itself is still a primitive value.

Why This Matters

APIs often use null to say no result, databases use it heavily, and JSON preserves it. Interviews use null to test equality rules, typeof quirks, and whether you can design clear missing-value semantics.

Theory

Intentional absence

Use null when your program deliberately sets a variable or property to no value. For example, a search result may be null when no user exists.

SituationPreferReason
No selected item yetnullAbsence is intentional state.
Optional parameter omittedundefinedJavaScript supplies it automatically.
Object property intentionally clearednullCommunicates deliberate emptiness.
JSON API field with no valuenullJSON supports null, not undefined.

typeof null quirk

typeof null returns 'object', but this is a historical bug. Do not use it as proof that null is an object. To check for object-like values, write value !== null && typeof value === 'object'.

Equality with undefined

null == undefined is true because loose equality treats them as the same kind of absence. null === undefined is false because they are different primitive values. In interviews and production code, prefer explicit checks unless you intentionally want to match both.

JSON behaviour

JSON.stringify keeps null object properties but omits properties whose value is undefined. In arrays, both undefined slots and null become null in JSON output.

Visual Diagrams

Absence semantics
undefined -> not provided / not initialised / missing by default
null      -> provided deliberately as empty

API response: { user: null } means the field exists and no user was found

Use `null` when absence is an intentional value in your domain model.

Code Examples

Checking for null explicitly

Strict equality makes the intent clear and avoids accidental matches with undefined.

Loading…

JSON preserves null

null is part of JSON. undefined is not a JSON value for object properties.

Loading…

Output Prediction

Predict the output #1

javascript
1console.log(typeof null);
2console.log(null == undefined);
3console.log(null === undefined);
4console.log(JSON.stringify({ a: null, b: undefined }));
5console.log(JSON.stringify([undefined, null]));

Interview Questions

1When would you use `null` instead of `undefined`?

Use null when absence is intentional and meaningful in the domain: no selected item, no database row found, or a field deliberately cleared. undefined is better for values JavaScript naturally leaves missing: uninitialised variables, omitted arguments, missing properties, or functions without returns.

Asked at:AmazonMicrosoftNetflix

Follow-ups

  • Why does `typeof null` return `object`?
  • How does JSON treat `null` and `undefined` differently?

Quiz

1. Which statement is correct?

Summary

  • `null` represents intentional absence and is a primitive value.
  • `typeof null` returns `'object'` because of a historical bug.
  • `null == undefined` is true, but `null === undefined` is false.
  • JSON preserves `null` object properties but omits `undefined` object properties.

Cheat Sheet

Meaning: deliberate empty value / no object / no result.

Type quirk: typeof null === 'object'.

Equality: null == undefined true; null === undefined false.

JSON: null is preserved; object properties with undefined are omitted.

Guard: value === null for exactly null; value == null only if you intentionally mean null or undefined.