Skip to content
beginner

null vs. undefined in JavaScript: Meaning and Common Sources

You log three things in the console. A variable you declared but never filled in. A property you hoped was on an object. A function call that was supposed…

Published 2026-10-03Updated 2026-10-047 min read
Vibrant sunrise over a tranquil countryside landscape with hills in the background.
Vibrant sunrise over a tranquil countryside landscape with hills in the background. Photo by Josias Salinas on Pexels.

You log three things in the console. A variable you declared but never filled in. A property you hoped was on an object. A function call that was supposed to hand you a result. The console answers undefined, undefined, and null. Same empty feeling, three different stories.

The weak model most beginners carry is: both mean empty, so they're interchangeable. That model survives right up until it costs you a bug. The stronger model is smaller and sharper: undefined is the value you observe when nothing was ever put in a slot. null is a value someone put there on purpose to mean "intentionally empty."

That wording matters. undefined is not forbidden from being assigned by hand — you can write let x = undefined and JavaScript will not complain. But in practice, undefined shows up mostly on its own, while null is almost always a deliberate choice. Treat that as the working distinction, not an absolute law.

Let's prove it with code you can run right now.

Two Kinds of Nothing

Side-by-side slots show undefined as an empty slot with no value set, and null as a slot containing an explicit null marker.
The key difference is whether emptiness was left by default or assigned on purpose.

Open your browser console or a Node.js session and type this:

let a;
let b = null;

console.log(a);
console.log(b);
undefined
null

Look at the two lines. They are not the same output, and that difference is the whole article in miniature.

a was declared but never assigned. JavaScript filled the gap with undefined — its way of saying "nothing was ever placed here." b was assigned null by you, deliberately. You said: this slot is empty, and I want it that way.

Both are primitive values. Both are falsy, meaning they behave like false inside an if check. But falsy and absent are not the same claim. A value can be falsy and still very much present — 0 and '' are proof of that. Hold onto this; it becomes a real bug later.

Knowledge check

Check your understanding

Answer this question before you continue.

What does this code log, in order?
Output Prediction

Focus: Predict the distinct values logged for an unassigned variable and a variable assigned null.

let a;
let b = null;
console.log(a);
console.log(b);

Where undefined Comes From

You will meet undefined in four everyday places. Recognizing them turns a scary word into useful information.

1. Declared but never assigned.

let score;
console.log(score);
undefined

This is the same "declare a variable, then give it a value" idea you already know from variables and data types — just seen from the empty side.

2. A missing object property.

const user = { name: "Ada" };
console.log(user.nickname);
undefined

The nickname key simply isn't there. JavaScript doesn't throw an error for a missing property; it hands you undefined. This is one of the most common sources of confusion when you're inspecting data.

3. A function that returns nothing.

function greet(name) {
  console.log("Hello, " + name);
}

const result = greet("Ada");
console.log(result);
Hello, Ada
undefined

The function ran. It printed. But it has no return statement, so the call itself produces undefined. If you ever see undefined where you expected a value from a function, check the return path first.

4. An array index that doesn't exist.

const colors = ["red", "green"];
console.log(colors[5]);
undefined

No error. Just undefined. The index is out of range, and JavaScript tells you so quietly.

Knowledge check

Check your understanding

Answer this question before you continue.

A function prints a message but has no return statement. What value does the function call produce?
Single Choice

Focus: Identify why a function call produces undefined when its function has no return statement.

Where null Comes From

null is almost always a deliberate signal. You or a library chose it.

You assign it yourself when a value genuinely isn't applicable yet:

let selectedUser = null;

You're saying: there is no selected user right now, and that's a meaningful state — not an accident.

APIs and databases send it for a field that exists but has no value. The key is present; the value is empty. This is different from a missing key, which arrives as undefined.

JSON serialization keeps null but drops undefined. Run this:

console.log(JSON.stringify({ a: null, b: undefined }));
{"a":null}

Notice b vanished entirely. That's why API payloads look the way they do — null survives the trip, undefined doesn't.

One quirk worth knowing: typeof null returns 'object'. This is a long-standing bug in JavaScript, not a hint that null is an object. Don't build logic on it.

Knowledge check

Check your understanding

Answer this question before you continue.

What does this expression log?
Output Prediction

Focus: Recognize that JSON serialization retains a null-valued object property but omits an undefined-valued one.

console.log(JSON.stringify({ a: null, b: undefined }));

Checking for null and undefined

Here's the working decision rule. Use === when you need to tell them apart:

let a;
let b = null;

console.log(a === undefined); // true
console.log(b === null);      // true
console.log(a === null);      // false
console.log(b === undefined); // false
true
true
false
false

Use == when you genuinely want "no value at all" and don't care which kind:

console.log(null == undefined);
true

That loose equality is useful sometimes and dangerous other times. The table below is the one that actually reduces confusion here.

Checknullundefined0''
value === nulltruefalsefalsefalse
value === undefinedfalsetruefalsefalse
value == nulltruetruefalsefalse
typeof value'object''undefined''number''string'
value ?? 'fallback''fallback''fallback'0''

That last row is the nullish coalescing operator ??. It supplies a fallback only when the left side is null or undefined. Unlike ||, it keeps 0 and '' — which matters more than you'd think.

const count = 0;
console.log(count || 10);  // 10  — wrong if 0 is valid
console.log(count ?? 10);  // 0   — correct
10
0

Knowledge check

Check your understanding

Answer this question before you continue.

Which check is true when value is undefined and false when value is null?
Single Choice

Focus: Choose a strict equality check that identifies undefined without also matching null.

Absence Is Not Falsiness

This is the misconception the whole article is circling. 0, '', false, null, and undefined are all falsy. But only the last two mean "no value."

An if (value) check silently lumps them together:

function showCount(count) {
  if (count) {
    console.log("Count: " + count);
  } else {
    console.log("No count provided");
  }
}

showCount(0);
No count provided

That's a bug waiting to happen. 0 is a legitimate count. The check confused "falsy" with "absent."

Decision rule: if you mean "is there a value here," test against null/undefined explicitly or use ??. If you mean "is this truthy," use the boolean check — and be sure that's what you actually want.

When to use null: for intentional emptiness you control. When not to: don't assign undefined by hand just to be tidy. It's legal, but it blurs the signal — you lose the ability to tell "JavaScript never set this" apart from "I set this to nothing." Let undefined surface naturally and reserve null for deliberate emptiness.

Common Beginner Mistakes

Using == and catching undefined by accident. Sometimes fine, sometimes the bug. If you only want null, use ===.

Trusting typeof for null checks. typeof null === 'object' will send you down a wrong path. Use === null instead.

Treating undefined as an error message. It usually isn't. It's information: a missing property, a forgotten return, or a typo in a key name. When you see it, log the value and its typeof, then trace where it was supposed to be assigned.

Where You'll Meet These in Real Code

Form inputs. A user who hasn't chosen a value yet is a natural null — the field exists, the choice doesn't.

Fetching data from an API. Missing keys arrive as undefined; explicit empties arrive as null. Knowing which is which tells you whether the field was ever meant to exist.

Function results. A search helper that finds nothing should return null deliberately, rather than falling off the end and returning undefined. The first says "I looked, nothing matched." The second says "I forgot to tell you anything."

What to Do Next

Carry this rule: undefined is the value you observe when nothing was set. null is a value someone set on purpose to mean empty.

Now open your console and predict before you press Enter. Declare a variable and log it. Read a property that isn't there. Call a function with no return. Guess each output first, then check. The prediction is where the learning happens.

Your next step is handling these values safely — default values with ?? and optional chaining with ?. — so a missing property doesn't crash your code before you can inspect it.

Knowledge check

Final check

Finish the article by checking the ideas you just learned.

When inspecting an API object, what is the article's practical distinction between a missing key and a key whose value is null?
Question 1 of 2Misconception Check

Focus: Interpret undefined and null from object data as different signals about whether a field is missing or explicitly empty.

Which check should you use to determine whether value is null?
Question 2 of 2Single Choice

Focus: Select a reliable check for null rather than relying on typeof.

References

  1. Nullish coalescing operator (??) - JavaScript | MDNdeveloper.mozilla.org
  2. ECMAScript® 2026 Language Specification262.ecma-international.org
Practical resource

Want a more structured JavaScript path?

Use the JavaScript for AI Applications Starter Pack to turn individual tutorials into a focused path from language fundamentals to interactive AI applications.

View the bundle
Coming soon

JavaScript for AI Applications Starter Pack

Build the JavaScript foundation behind modern interactive applications. The JavaScript for AI Applications Starter Pack takes you from core language concepts through functions, browser events, forms, DOM updates, application state, debugging, and practical projects—so you can learn how interfaces take input, work with structured data, respond to users, and turn results into experiences people can actually use.

$9
PDF BundleJavaScriptWeb DevelopmentAI Applications
  • 253-page illustrated PDF
  • 12 guided JavaScript chapters
  • Visual concept diagrams
  • Self-assessment quizzes
  • Bonus deep-dive sections
  • DOM, events, forms & application state
  • Browser projects, debugging & practical workflows

Coming soon

Keep learning

Related tutorials

Continue with nearby JavaScript topics and beginner-friendly explanations.

A library shelf filled with colorful children's books, focused on educational topics.
beginner
9 min read

Arrays and Objects Basics

A variable can hold one value—and that value can be a collection holding many related values. JavaScript arrays and objects are how you build those…

Read tutorial
University student studies alone in a sunlit classroom, Buenos Aires, Argentina.
beginner
9 min read

Basic Operators in JavaScript

You've learned how to store values in variables. Now it's time to make those values do something. JavaScript operators are the verbs of your code—they add,…

Read tutorial