completefrontend
Open lab My progress
THE JAVASCRIPT FIELD GUIDE

Booleans, null & undefined

Learn true, false, JavaScript’s eight falsy values, and the difference between intentional null and not-yet-filled undefined.

By the end, you can
  • 01
    Tell truthy from falsyName the eight falsy values, and explain why values like [] are truthy.
  • 02
    Tell null from undefinedUse null for "empty on purpose" and know when JavaScript gives undefined.
  • 03
    Check for missing values safelyWrite checks that don't treat 0 or "" as missing.

Yes, no, and no value

Programs often need a plain yes or no. Is the menu open? Did the user fill in the form? Should this message show? JavaScript has two values for this, called booleans:

  • true means yes.
  • false means no.
Two booleansPop out in the code editor (opens in a new tab)JavaScript
const subscribed = true;const muted = false;console.log(subscribed);console.log(muted);

Line 1 stores true in subscribed. Line 2 stores false in muted. Lines 3 and 4 print them. These are real booleans, not the text "true" or "false".

Most of the time you don't type true yourself. You ask a question, and JavaScript answers with a boolean. A comparison, like 3 >= 2, is one such question.

A comparison gives a booleanPop out in the code editor (opens in a new tab)JavaScript
const hasEnoughTea = 3 >= 2;const hasEnoughMilk = 1 >= 2; console.log(hasEnoughTea);console.log(hasEnoughMilk);

Line 1 asks "is 3 at least 2?" The answer is yes, so hasEnoughTea holds true. Line 2 asks "is 1 at least 2?" The answer is no, so hasEnoughMilk holds false. Lines 4 and 5 print true, then false. A name like hasEnoughTea reads like a question. That makes the code easy to read later.

This lesson also looks at two other values: null and undefined. They are not booleans. But they often show up next to yes/no checks, because code must also ask, "Do I have a value at all?" Each of the three has its own lesson:

Here you put them side by side and learn which check to use for each job.

Definition
  • A boolean is one of two values: true or false.
  • A truthy value is any value that an if treats like true.
  • A falsy value is any value that an if treats like false.
  • null means "empty on purpose". Code puts it there.
  • undefined means "no value was given yet".
Real-life analogyA guest list with a short no list

A guard at a party has a short no list with a few names on it. People on the list stay out. Everyone else gets in. The guard only checks the list. A name tag that says "false" still gets in, because that name is not on the list.

In real life: The few names on the no list
In JavaScript: The eight falsy values
In real life: Everyone else gets in
In JavaScript: Every other value is truthy
In real life: A name tag that says "false"
In JavaScript: The string "false" is truthy

This lesson uses tiny if statements, because they show truthiness most clearly. An if runs its code only when its condition counts as yes. You'll learn if fully in a later lesson. For now, read if (value) as "if this value counts as true".

Truthy and falsy values

INTERACTIVE

The condition of an if does not have to be true or false. It can be any value. JavaScript first turns the value into a boolean, then decides. Only eight values turn into false. They are the falsy values, and the list is short enough to learn by heart:

  • false
  • 0, the number zero
  • -0, negative zero
  • 0n, zero as a BigInt (a whole-number type you'll meet in the next lesson)
  • "", the empty string (text with no characters)
  • null
  • undefined
  • NaN, which means "not a number"

Every other value is truthy. That includes values that look empty or false: "0", "false", " ", [], , -1, and Infinity.

Left: the eight falsy values, false, 0, -0, 0n, the empty string, null, undefined, and NaN. Right: values that look empty or false, "0", "false", " ", [], {}, and -1. They are all truthy, like every other value.falsy: only these 8false0-00n""nullundefinedNaNtruthy: all the rest"0""false"" "[]{}-1and every other value
The left side is the whole falsy list. An if treats these eight values like false. The right side shows values that only look empty or false. They are not on the list, so they are truthy.

So if (value) is handy for asking "is there something here?" But be careful when 0 or an empty string is a real answer. The check treats them as "nothing".

A failed number is falsy, but check for it by namePop out in the code editor (opens in a new tab)JavaScript
const price = Number("tea"); console.log(Number.isNaN(price));console.log(Boolean(price));

Line 1 asks Number("tea") to turn the text "tea" into a number. It can't, so it gives NaN. Line 3 uses Number.isNaN(price), which asks "is this NaN?" It prints true. Line 4 prints false, because NaN is falsy.

A falsy check only tells you "this counts as no". It can't tell NaN apart from 0 or null. When you need to know that a number failed, use Number.isNaN.

Truthy or falsy?
  • false
  • 0
  • -0
  • 0n
  • ""
  • null
  • undefined
  • NaN
  • "0"
  • "false"
  • " "
  • []
  • {}
  • -1
  • Infinity
Try it yourself
0 of 15 correct

Put each value on the side where it belongs. Only the eight values on the falsy list are falsy.

Choose a category for every card. You can change an answer at any time; Reset clears them all.

Test truthiness for real

PLAYGROUND

Pick a value. The code below tests it in three ways, and all three always agree:

  • Boolean(value) turns the value into true or false.
  • !!value does the same thing. One ! gives the opposite boolean. A second ! flips it back.
  • if (value) runs the truthy branch or the falsy branch. A branch is the block of code that runs.
Truthiness tester
Test one value three waysPop out in the code editor (opens in a new tab)JavaScript
const value = false;console.log(Boolean(value));console.log(!!value);if (value) {  console.log("truthy branch");} else {  console.log("falsy branch");}
Real resultsfalsy
Boolean(value)false
!!valuefalse
Branch that runsfalsy branch
Whyfalse is the boolean that means no.
Try it yourself

false is falsy. Boolean(value) gives false, !!value gives false, and the if statement runs the falsy branch. false is the boolean that means no.

Line 1 changes to the value you pick. The results come from running the real code in JavaScript.

Try [] and . An empty array is still an array, and an empty object is still an object. Every array and object is truthy, even with nothing inside. To ask "does this list have items?", check its length, the number of items. Don't test the array itself.

null vs undefined

COMPARE

null and undefined both mean "there is no useful value here". But they mean it in different ways:

  • null means the value is empty on purpose. Some code chose to put null there.
  • undefined means no value was given yet. Usually JavaScript gives you undefined by itself.
Real-life analogyA form field: cleared or never filled in

Think of a profile form. You click "Remove photo", and the photo field is now empty because you chose that. Another field, "Nickname", is empty because nobody has typed anything there yet. Both fields are empty, for two different reasons.

In real life: You clear the field on purpose
In JavaScript: Your code stores null
In real life: Nobody has filled in the field yet
In JavaScript: A missing property or argument is undefined
In real life: The cleared field tells others you chose empty
In JavaScript: null shows a choice
Left: photoUrl = null puts null in a box called photoUrl, to say it is empty on purpose. Right: let nickname; makes a box called nickname that holds undefined, because nothing was put in yet.photoUrl = nullnullphotoUrlcleared on purposelet nickname;undefinednicknamenot filled in yet
Both boxes have no useful value. On the left, the code put null in the box on purpose. On the right, the code never put anything in, so JavaScript gives undefined.

The table compares the two values. Read each row as a question, then the answer for null and for undefined.

How null and undefined differ
Questionnullundefined
What does it mean?The value is empty on purpose.No value was given yet.
Who usually puts it there?Your code, or a tool that tells you it found nothing.JavaScript, when a value is missing.
What does typeof give?"object". This is an old mistake in JavaScript."undefined".
Does loose equals == call them equal?Yes. null == undefined is true.Yes. undefined == null is true.
Does strict equals === call them equal?No. null === undefined is false.No. undefined === null is false.
What does JSON.stringify do with it inside an object?It keeps it as null.It leaves the property out.

You saw typeof null in the Data types lesson. typeof tells you the type of a value, and for null it gives "object". This is an old mistake that was never fixed. null is not an object, and you can't read properties from it.

A property set to undefined is still therePop out in the code editor (opens in a new tab)JavaScript
const profile = {  nickname: undefined,  photoUrl: null,}; console.log("nickname" in profile);console.log(profile.nickname === undefined);console.log("age" in profile);console.log(profile.age === undefined);console.log(profile.photoUrl === null);

Lines 1 to 4 make a profile object. It has a nickname property that holds undefined, and a photoUrl property that holds null. It has no age property at all.

The in operator asks "does this object have a property with this name?" Line 6 prints true: nickname is there, even though its value is undefined. Line 8 prints false: age was never added. But lines 7 and 9 both print true, because reading nickname or age gives undefined either way. So reading a property can't tell you whether it is there. Use in for that.

Check the object's own propertiesPop out in the code editor (opens in a new tab)JavaScript
const profile = {  nickname: undefined,}; console.log(Object.hasOwn(profile, "nickname"));console.log(Object.hasOwn(profile, "age"));

Object.hasOwn(profile, "nickname") asks the same question in another way: "does profile itself have a property called nickname?" Line 5 prints true, and line 6 prints false. For plain data, like data from a form or a server, Object.hasOwn is often the clearer choice.

JSON keeps null and leaves out undefinedPop out in the code editor (opens in a new tab)JavaScript
const profile = {  nickname: undefined,  photoUrl: null,}; console.log(JSON.stringify(profile));console.log(JSON.stringify([undefined, null]));console.log(JSON.stringify(undefined));

JSON is a text format for sending data, for example to a server. JSON.stringify turns a value into JSON text. Here is what each line prints:

  1. Line 6 prints {"photoUrl":null}. JSON keeps a property that holds null, but leaves out a property that holds undefined.
  2. Line 7 prints [null,null]. An array must keep every position, so JSON writes undefined as null there.
  3. Line 8 prints undefined. There is no JSON text for undefined on its own, so the call gives back undefined instead of text.
A default value fills in for undefined, not for nullPop out in the code editor (opens in a new tab)JavaScript
function greeting(name = "friend") {  return "Hi, " + name;} console.log(greeting());console.log(greeting(null));

On line 1, name = "friend" is a default value. It is used when the caller gives no name. Line 5 calls greeting() with nothing, so name is undefined, the default fills in, and it prints "Hi, friend". Line 6 passes null. A default value only fills in for undefined, so it prints "Hi, null". If null should get "friend" too, use ?? inside the function. You'll meet ?? later in this lesson.

Where missing values appear

STEP THROUGH

You get undefined in many everyday places. Here are five of them:

  • a variable made with let but given no value,
  • an object property that doesn't exist,
  • a function that has no return,
  • a function argument the caller left out,
  • an array position past the end of the array.

null shows up when code puts it there on purpose. Some browser tools also give null when they find nothing. For example, document.querySelector(".missing") searches the page for an element. When nothing matches, it gives null. In the Talking to the user lesson, prompt also gives null when the user clicks Cancel.

Step through the code below. Watch each line make an undefined, and the last one make a null.

Five ways to get undefined, and one null
Step 0 of 10Ready
Your turn: follow the blue line

Step through five everyday ways to get undefined, then one null that the code chose on purpose.

Running in
  1. script
Next: line 1
const profile = { name: "Ada" };function noReturn() {  const message = "done";}function echo(nickname) {  return nickname;}const list = ["first"];const deliberatelyEmpty = null; console.log(notFilled);console.log(profile.age);console.log(noReturn());console.log(echo());console.log(list[3]);console.log(deliberatelyEmpty);
CallStoreChangeResultBlue line: runs next. Ran: already executed.
Recent returnsNothing yet. Start with the blue line.
A guided replay recorded from real JavaScript calls, not an engine debugger. Step follows executed statements; Back reviews a snapshot. Reset starts a fresh run.
find gives undefined when nothing matchesPop out in the code editor (opens in a new tab)JavaScript
const todos = ["tea", "milk"];const missingTodo = todos.find((todo) => todo === "bread"); console.log(missingTodo);

Line 1 makes a list of two to-dos. On line 2, find looks for the item "bread". It isn't in the list, and find gives undefined when nothing matches. So line 4 prints undefined. Other tools, like querySelector, give null instead. Don't guess. Look up what a tool gives when it finds nothing, then write a check for that value.

Checking for missing values

STEP THROUGH

A common beginner bug is to treat "falsy" as if it meant "missing". Say a quantity can be 0, or a name can be an empty string. Then if (value) catches too much: it treats 0 and "" as missing too.

The step-through below runs four checks on the same value. Pick a value, then step through the lines:

  • Line 2, Boolean(name), is the truthy check.
  • Line 3, name === undefined, is true only for undefined.
  • Line 4, name == null, is true for null and undefined, and nothing else.
  • Line 5, name ?? "friend", uses "friend" only when name is null or undefined.
Four checks on the same value
Step 0 of 5Ready
Your turn: follow the blue line

Pick a value, then compare four checks: the truthy check, === undefined, == null, and ??.

Running in
  1. script
Next: line 1
console.log(Boolean(name));console.log(name === undefined);console.log(name == null);console.log(name ?? "friend");
CallStoreChangeResultBlue line: runs next. Ran: already executed.
Recent returnsNothing yet. Start with the blue line.
Value to test

A new value starts the steps again from line 1. Watch what happens with 0 and "".

A guided replay recorded from real JavaScript calls, not an engine debugger. Step follows executed statements; Back reviews a snapshot. Reset starts a fresh run.

Two of these checks are the ones to remember:

  • Use value == null when null and undefined should both count as missing, and nothing else should.
  • Use value ?? fallback when you want a backup value only for null or undefined. ?? is called the nullish coalescing operator. "Nullish" means "null or undefined".

The Operators module has full lessons on comparisons, ??, and the related ?. operator.

Choose a fallback carefully

A fallback is a backup value. Your program uses it when the real value isn't there. Two operators can give a fallback, and they decide differently:

  • || uses the fallback for every falsy value.
  • ?? uses the fallback only for null and undefined.
An empty name is not always a missing namePop out in the code editor (opens in a new tab)JavaScript
const savedValue = "";const withOr = savedValue || "guest";const withNullish = savedValue ?? "guest"; console.log(withOr);console.log(withNullish);

Line 1 stores an empty string in savedValue. In a form, an empty name might be a real choice. Line 2 uses ||. The empty string is falsy, so withOr gets "guest", and line 5 prints guest. Line 3 uses ??. The empty string is not null or undefined, so withNullish keeps it. Line 6 prints the empty string, so the console shows a blank line.

Left: savedValue || "guest" with an empty string gives "guest", so the empty string is replaced. Right: savedValue ?? "guest" with an empty string gives the empty string, so it is kept.savedValue || "guest""""guest"saved valueresult"" is replacedsavedValue ?? "guest"""""saved valueresult"" is kept
The saved value is the empty string "" in both halves. || replaces every falsy value, so it gives "guest". ?? replaces only null and undefined, so it keeps "".

The table tries five saved values. Read each row as: "when savedValue is this, each line gives that."

When || and ?? use the fallback
When savedValue issavedValue || "guest" givessavedValue ?? "guest" gives
undefined"guest""guest"
null"guest""guest"
0"guest"0
"""guest"""
false"guest"false

Use || when every falsy value should mean "use the backup". For example, a search box where empty text should never count. Use ?? when 0, an empty string, or false is real data that the page should keep.

Where you'll use this

Real web pages use these checks all the time. Some examples:

  • form fields the user may leave empty,
  • how many of an item are in a shopping cart,
  • settings that are on or off,
  • search filters,
  • data sent back from a server.

Each time, ask yourself one question: "Am I checking yes or no? Or am I checking whether a value is missing?"

An empty form field gives text, not a numberPop out in the code editor (opens in a new tab)JavaScript
const quantityText = ""; console.log(quantityText === "");console.log(Number(quantityText));

A form field in the browser always gives you text, called a string. Line 1 stands for an empty quantity field: quantityText is "". Line 3 prints true, so the field is empty. Line 4 prints 0, because Number("") turns an empty string into zero. So check the text first. Otherwise an empty field looks the same as a quantity of zero.

Keep zero as a real quantityPop out in the code editor (opens in a new tab)JavaScript
function labelQuantity(quantity) {  if (quantity == null) {    return "missing";  }  return quantity + " selected";}console.log(labelQuantity(0));

Line 2 checks quantity == null. That is true only for null and undefined. So labelQuantity(0) skips the "missing" line and returns "0 selected".

Keep an empty name from the server, replace a missing photoPop out in the code editor (opens in a new tab)JavaScript
const response = {  displayName: "",  photoUrl: null,}; const name = response.displayName ?? "Guest";const photo = response.photoUrl ?? "default-photo.png"; console.log(name === "");console.log(photo);

Lines 1 to 4 stand for data sent back from a server. The displayName is an empty string. The photoUrl is null, which means there is no photo. Line 6 keeps the empty name, because "" is not null or undefined. Line 7 uses the backup photo, because photoUrl is null. Line 9 prints true, and line 10 prints default-photo.png.

A boolean can say whether a menu is openPop out in the code editor (opens in a new tab)JavaScript
const isMenuOpen = false;const buttonLabel = isMenuOpen ? "Close menu" : "Open menu"; console.log(buttonLabel);

Line 1 stores one fact: the menu is closed. Line 2 uses ? :, which picks one of two values. If isMenuOpen is truthy it picks "Close menu". Otherwise it picks "Open menu". So line 4 prints Open menu. A boolean is a good fit when something has exactly two states. If it can be in three or more states, use a value with more choices, like a string.

Common mistakes

  • "Empty arrays are falsy." They are truthy, like every array. Check array.length instead.
  • "The string "false" is false." It is text with five characters. Only the empty string is falsy, so it is truthy.
  • "null and undefined are the same." null == undefined is true, but null === undefined is false. They also mean different things.
  • "typeof null shows that null is an object." It is an old mistake in JavaScript. null has no properties to read.
  • "if (value) is a safe check for a missing value." It also treats 0, "", false, and NaN as missing.

The code below shows the first mistake. Line 3 tests the array itself. Line 7 tests how many items it has.

An empty array still counts as truePop out in the code editor (opens in a new tab)JavaScript
const cartItems = []; if (cartItems) {  console.log("cart exists");} if (cartItems.length === 0) {  console.log("cart is empty");}

cartItems is an empty array. Line 3 asks if (cartItems). An array is always truthy, so line 4 prints cart exists. Line 7 asks whether cartItems.length is 0. It is, so line 8 prints cart is empty.

Practice exercises

5 EXERCISES
Exercise 1 · Warm-upPredict three truthy checks

Predict what each line prints, in order. Put commas between the answers.

Starter codePop out in the code editor (opens in a new tab)JavaScript
console.log(Boolean("0"));
console.log(Boolean(""));
console.log(Boolean([]));

Answer, then press Check. Spacing and letter case don’t matter.

    Exercise 2 · PracticeFix the zero quantity bug

    The function says 0 is missing. Change the check on line 2 so that zero counts as a real quantity.

    Starter codePop out in the code editor (opens in a new tab)JavaScript
    function labelQuantity(quantity) {
      if (!quantity) {
        return "missing";
      }
      return quantity + " selected";
    }
    console.log(labelQuantity(0));

    Answer, then press Check. Spacing and letter case don’t matter.

      Exercise 3 · PracticeChoose null or undefined

      A user clicks "Clear photo". Should the app store null, or leave the field undefined?

      Answer, then press Check. Spacing and letter case don’t matter.

        Exercise 4 · PracticeWrite a fallback for null

        Which operator makes displayName(null) give "friend", but keeps "" as it is?

        Starter codePop out in the code editor (opens in a new tab)JavaScript
        function displayName(name) {
          return name ?? "friend";
        }
        console.log(displayName(null));
        console.log(displayName(""));

        Answer, then press Check. Spacing and letter case don’t matter.

          Exercise 5 · ChallengeExplain the empty array mistake

          Which property should code check to find out whether a list is empty?

          Starter codePop out in the code editor (opens in a new tab)JavaScript
          const items = [];
          console.log(Boolean(items));
          console.log(items.length === 0);

          Answer, then press Check. Spacing and letter case don’t matter.

            Check your understanding

            7 QUESTIONS
            Booleans, null & undefined quiz · 7 questionsScore: first tries count
            1. Question 1 of 7How many values are on JavaScript's falsy list?

              Choose an answer to see the explanation.

            2. Question 2 of 7What does Boolean("false") print?

              Read the code, then predictPop out in the code editor (opens in a new tab)JavaScript
              console.log(Boolean("false"));

              Choose an answer to see the explanation.

            3. Question 3 of 7What does Boolean([]) print?

              Read the code, then predictPop out in the code editor (opens in a new tab)JavaScript
              console.log(Boolean([]));

              Choose an answer to see the explanation.

            4. Question 4 of 7Which sentence best describes null?

              Choose an answer to see the explanation.

            5. Question 5 of 7What do these two comparisons print?

              Read the code, then predictPop out in the code editor (opens in a new tab)JavaScript
              console.log(null == undefined);
              console.log(null === undefined);

              Choose an answer to see the explanation.

            6. Question 6 of 7What does ?? do with zero?

              Read the code, then predictPop out in the code editor (opens in a new tab)JavaScript
              const quantity = 0;
              console.log(quantity ?? 1);

              Choose an answer to see the explanation.

            7. Question 7 of 7Which check counts null and undefined as missing, but not 0 or ""?

              Choose an answer to see the explanation.

            Key takeaways

            • There are exactly two booleans: true and false.
            • There are exactly eight falsy values: false, 0, -0, 0n, "", null, undefined, and NaN.
            • Every object is truthy. That includes [] and .
            • Use null to say "empty on purpose". Expect undefined when no value was given.
            • When you mean "missing" and not just "falsy", use == null or ??.

            In one line: Truthiness is how JavaScript reads any value as yes or no. null and undefined are its two ways to say "no value here".

            Up next: BigInt, a type for very large whole numbers.

            CompleteFrontend Clear concepts. Working examples.