Null and Undefined in JavaScript

Introduction

undefined and null both represent “no useful value,” but they mean different things in practice. Mixing them causes subtle bugs in APIs and forms. This chapter explains when each appears, how to check them safely, and how modern operators (??, ?.) help.

Prerequisites

undefined

undefined means not assigned, missing property, or no return value:

javascript
// Variable declared without initializer
let nickname;
console.log(nickname);
 
// Missing object property
const profile = { name: "Ada" };
console.log(profile.age);
 
// Function with no return
function noop() {}
console.log(noop());

null

null is an intentional “empty” or “unknown” value set by developers:

javascript
// Explicitly no selected user
const selectedUser = null;
 
if (selectedUser === null) {
  console.log("No user selected");
}

Comparing null and undefined

undefinednull
Typical sourceEngine/defaultDeveloper assignment
typeof"undefined""object" (quirk)
Loose equalitynull == undefined is truesame
Strict equalitynull === undefined is false
javascript
// Strict checks (recommended)
const value = null;
 
console.log(value === null);
console.log(value === undefined);

Tip

Best Practice

Use === null or === undefined explicitly instead of == when you care which one you received.

Default Values: || vs ??

javascript
// || treats 0 and "" as missing (falsy)
const count = 0;
const withOr = count || 10;
console.log(withOr);
 
// ?? only replaces null or undefined
const withNullish = count ?? 10;
console.log(withNullish);

Use ?? when 0, false, or "" are valid data.

Optional Chaining ?. (Preview)

Safely access nested data when intermediate values may be nullish:

javascript
// Optional chaining avoids TypeError on null/undefined
const response = { user: { name: "Lin" } };
console.log(response.user?.name);
console.log(response.manager?.name);

Full patterns in operators.

JSON and APIs

JSON has null but no undefined. Missing JSON fields become undefined when parsed in some pipelines—know your API contracts.

javascript
// JSON.parse example
const json = '{"title":"Post","author":null}';
const data = JSON.parse(json);
console.log(data.author);
console.log(data.publishedAt);

Mini Example: Nullable Config

javascript
// Merge user config with defaults
const userConfig = {
  theme: null,
  fontSize: 0,
};
 
const defaults = {
  theme: "light",
  fontSize: 14,
  lang: "en",
};
 
const theme = userConfig.theme ?? defaults.theme;
const fontSize = userConfig.fontSize ?? defaults.fontSize;
const lang = userConfig.lang ?? defaults.lang;
 
console.log({ theme, fontSize, lang });

FAQ

Should I use null or undefined in my APIs?

Teams differ. Be consistent: many use null in JSON for absent values and undefined only inside JavaScript logic.

Is void 0 the same as undefined?

Historically yes in many environments—avoid void 0 in new code; use undefined.

How do I remove a property?

delete obj.prop or set to undefined depending on semantics—objects chapter covers details.

What comes next?

Type conversion, then operators and conditionals.