Programs, values, and types · Lesson 7 of 77 · concept

undefined, null, and naming every type

About 45 minutes.

By the end of this lesson you can

  • Say what undefined means and where it comes from without you writing it.
  • Say what null means and why someone had to choose it deliberately.
  • Use typeof to find out what kind of value you are holding.
  • List the kinds of value JavaScript has, and name the one typeof lies about.

JavaScript has two different kinds of nothing

Explanation

Most languages have one way of saying there is nothing here. JavaScript has two, and the difference between them is a difference of authorship: undefined is what the language leaves behind when nobody supplied a value, and null is what a programmer writes down when they mean deliberately empty.

The distinction is not academic. An address field holding undefined says nobody has filled this in yet. The same field holding null says a person considered it and recorded that there is no address. One is a gap in the process; the other is a fact about the world.

undefined turns up on its own

Explanation

You will rarely type undefined. It arrives by itself, in exactly the situations where a value was expected and none was ever provided: a let declared with no value yet, a property asked for on an object that does not have it, a function that finished without handing anything back — including console.log, which is why the earlier lesson said it gives you nothing you can use.

That is why undefined so often shows up as the first symptom of a bug elsewhere. It is rarely wrong in itself; it is a message that something upstream never happened. When you see it, the useful question is not what is undefined but which step was supposed to fill this in.

null is a decision somebody made

Explanation

null never appears by accident. It is in your program because somebody wrote it, which makes it a record of intent: we looked, and there is nothing, and that is the final answer.

Use it when absence is a real, meaningful state — no middle name, no discount applied, no manager for this employee. Reach for it sparingly. A value that is sometimes a number and sometimes null forces every piece of code downstream to cope with both, and the module on errors and robustness looks hard at when that cost is worth paying.

typeof: asking a value what it is

Explanation

typeof is an operator, not a function — it goes in front of a value with a space, and the brackets you often see around its operand are just ordinary grouping brackets. What it hands back is a piece of TEXT naming the kind of value: the six characters n-u-m-b-e-r, not the number type itself. So typeof 42 === "number" is a comparison of two strings, and it is true.

Here is the full set of kinds JavaScript has. Seven of them are PRIMITIVE — simple, single values that cannot be taken apart: undefined, null, boolean, number, string, plus bigint for very large whole numbers and symbol for unique keys. Everything else is an OBJECT: arrays, dates, the collections in module 4, and functions, which are objects you can call.

typeof reports those honestly with two footnotes. Functions get their own answer, "function", even though a function is an object. And null reports as "object", which is simply wrong — it is a bug from the first version of JavaScript in 1995 that cannot be fixed now without breaking a large fraction of the web. Everybody who writes JavaScript knows this one; you now do too.

Where the two nothings bite

A common mistake

Testing for null with typeof. Because typeof null is "object", a check written as typeof value === "object" is true for null, and code that then reaches inside the value stops with an error. Compare with value === null instead; it is exact and it is short.

Quoting them. The text "undefined" and the value undefined are as different as the text "7" and the number 7. A check against the quoted version passes only for a value that is genuinely a piece of text spelling out the word, which is almost never what you meant.

Assuming a name exists. Asking for a name that was never declared normally stops the program — but typeof is the one operator allowed to ask about a name that does not exist, and it answers "undefined" rather than failing. That is occasionally useful and frequently confusing, because it means a typo in a name can look like an absent value rather than a mistake.

Why this is where real programs actually break

Why it matters

Look at the errors real applications report and a large share are one sentence: something was expected and nothing was there. Not a wrong calculation — an absence, arriving somewhere that assumed a presence.

Everything later in this course that deals with that problem — optional chaining, default values, validating at the boundary, returning an outcome instead of throwing — is built on the distinction you have just learned. Knowing which nothing you have, and whether it means not yet or none, is what makes those tools make sense rather than seeming like syntax to memorise.

Recap

Recap

undefined is the language's placeholder when no value was ever supplied; null is a programmer's deliberate mark of emptiness. typeof produces a piece of text naming a kind of value. There are seven primitive kinds — undefined, null, boolean, number, string, bigint, symbol — and everything else is an object. typeof null says "object", which is a preserved 1995 bug; test with === null instead.

Worked examples

Every output below was produced by actually running the code in the same sandbox your exercises run in — it is not a prediction.

typeof, applied to one of everything

Seven questions and seven answers, all of them text. Two answers are worth remembering rather than deriving: null and the name that was never declared.

1let notAssigned;2console.log(typeof notAssigned);3console.log(typeof 42);4console.log(typeof "hi");5console.log(typeof true);6console.log(typeof null);7console.log(typeof {});8console.log(typeof neverDeclaredAnywhere);
  • line 1

    Declared with no value, so it holds undefined. Nobody wrote that; the language supplied it.

  • line 6

    The famous wrong answer. null is not an object, and this cannot be fixed.

  • line 7

    An empty object — the collection type of module 4. Inside brackets like this it is a value, not a block of code.

  • line 8

    This name was never declared. Any other operator would stop the program here; typeof is allowed to ask and answers undefined.

What it prints

undefinednumberstringbooleanobjectobjectundefined

Telling the two nothings apart

Two fields on a form: one never touched, one deliberately marked as having no value. They print differently and they compare differently.

1let middleName;2const nickname = null;3 4console.log(middleName);5console.log(nickname);6console.log(middleName === null);7console.log(middleName === undefined);8console.log(nickname === null);
  • line 1

    Never assigned, so it is undefined: nobody has filled this in.

  • line 2

    Assigned null on purpose: we asked, and there is no nickname.

  • line 6

    false. The two nothings are genuinely different values, and === does not blur them.

  • line 7

    true, and this is how you check for undefined exactly. Note that === can compare against undefined perfectly well; it is only typeof that has the awkward edge case.

What it prints

undefinednullfalsetruetrue

Check yourself

Not scored, not stored. Getting one wrong is the useful part.

One of these two lines prints the answer people expect. Which, and what do they print?

console.log(typeof null);console.log(typeof "null");

A user record has no middle name. Which value best records that the user was asked and has none?

Which check is true for null and ONLY for null?

Write it yourself

Exercise · core · 5 tests

Two kinds of empty on one form

A sign-up form has two fields. middleName was never touched by the user, so it must be declared with let and given no value at all. nickname was offered and the user said they do not have one, so it must be null. Then record what typeof says about each: typeOfMiddleName and typeOfNickname.

What your code must do

middleName holds undefined because it was declared without a value — do not assign undefined by hand, leave the declaration bare. nickname is exactly null. typeOfMiddleName is the text undefined and typeOfNickname is the text object, which is the historical bug rather than a mistake on your part.

Define nickname, typeOfMiddleName and typeOfNickname at the top level of your code — the tests call them by name.

Runs on your device in a sandbox with no network and no access to this page.

Worth remembering

  • What is the difference between undefined and null?

    undefined is left by the language when no value was ever supplied. null is written by a programmer to record deliberate emptiness. They are different values and === tells them apart.

  • What does typeof null return, and what should you use instead?

    The text "object" — a bug preserved since 1995 for compatibility. Test with value === null.

  • Name the seven primitive types.

    undefined, null, boolean, number, string, bigint and symbol. Everything else — arrays, dates, functions — is an object.

Check this lesson against the source

We wrote the explanation; we did not invent the facts. This is the page that backs them.

MDN — typeof

Check there: The table of return values — including typeof null returning "object" — and the section explaining that this is a bug kept for compatibility.

MDN — JavaScript data types and data structures

Check there: The list of the seven primitive types (undefined, null, boolean, number, bigint, string, symbol) and the statement that everything else is an object.

MDN — null

Check there: That null represents the intentional absence of any object value.

Prefer to read the source in a structured breakdown? Turn this doc into a breakdown

0 of 1 exercise in this lesson solved on this device

Consent version 2026-07-31.1

Cookie preferences