JavaScript AcademyProfessional Engineering
Level 1 · FoundationsModule 2: Variables, Values and TypesLesson 9 of 150About 40 minutes

Lesson 9: Booleans, null and undefined

01Opening question

A form asks for an optional phone number. How should a program say "no value" without confusing it with an empty phone number or zero?

Keep the question in mind. By the end of the lesson you will be able to answer it, and prove it with working code.

02Start with working code

A declared variable without a value is undefined. null is a value you choose on purpose to mean "nothing".

const isLoggedIn = true;
let coupon;
const middleName = null;
console.log(isLoggedIn, coupon, middleName);

03Objective and prerequisites

Learning objective: Use booleans, understand truthy and falsy values, and tell undefined (not assigned) from null (deliberately empty).

You need: Lesson 6: Variables with let and const

Skills practised: Values and variables, Booleans, null and undefined.

04Explanation

A boolean has only two values: true and false. Comparisons produce booleans: 5 > 3 is true, cartTotal === 0 is true or false.

undefined means a value was never given: a variable declared without a value, or a missing property. null means "empty on purpose": the program decided there is no value.

Every value is either truthy or falsy when JavaScript needs a yes/no answer. The falsy values are false, 0, "" (empty string), null, undefined and NaN. Everything else, including "0" and "false", is truthy.

Boolean(value) shows whether a value is truthy, and !value flips it. This matters for checks such as "did the user type anything?"

05Definitions

Boolean
A value that is either true or false.
undefined
The value of something that has not been given a value.
null
A value that means "intentionally empty".
Truthy
A value that counts as true in a condition.
Falsy
A value that counts as false: false, 0, "", null, undefined and NaN.
Logical NOT (!)
Flips a value's truthiness: !true is false.

Open the full glossary

06Visual learning object

Data type inspector. Inspect true, null, undefined, 0 and "" and compare their types and truthiness.

Loading the interactive object…

Open the full Data type inspector page with its guided experiment.

07Examples

ex-009-01Booleans from comparisons Browser-executable JavaScript

Comparisons answer yes/no questions with true or false.

const stock = 4;
const ordered = 6;
const hasEnough = stock >= ordered;
console.log(hasEnough);
console.log(stock > 0);
console.log(typeof hasEnough);
Sample input
None
Expected output
false true boolean
Common mistake
Storing "true" in quotes, which is a string, not a boolean.
Try this
Create isFreeShipping that is true when a total of 60 is at least 50.
Source
examples/ex-009-01-booleans-from-comparisons.js

ex-009-02undefined versus null Browser-executable JavaScript

undefined appears when nothing was assigned; null is assigned on purpose.

let nickname;
const fax = null;
console.log(nickname);
console.log(fax);
console.log(typeof nickname, typeof fax);
const user = { name: "Ada" };
console.log(user.email);
Sample input
None
Expected output
undefined null undefined object undefined
Common mistake
Assuming typeof null is "null"; it is "object".
Try this
Set fax to "555-0100" and run again.
Source
examples/ex-009-02-undefined-vs-null.js

ex-009-03Truthy and falsy Browser-executable JavaScript

Boolean(x) shows how a value behaves in a condition. Only six values are falsy.

console.log(Boolean(0), Boolean(""), Boolean(null), Boolean(undefined), Boolean(NaN));
console.log(Boolean(42), Boolean("0"), Boolean("false"), Boolean(" "));
console.log(!0, !"hello");
Sample input
None
Expected output
false false false false false true true true true true false
Common mistake
Believing "0" or "false" are falsy; non-empty strings are truthy.
Try this
Check Boolean([]) and Boolean({}). Are empty lists truthy?
Source
examples/ex-009-03-truthy-falsy.js

ex-009-04When 0 is a real value Browser-executable JavaScript

A quantity of 0 is valid data, but a truthiness check treats it as missing. ?? only replaces null and undefined.

const quantity = 0;
console.log(quantity || "not set");
console.log(quantity ?? "not set");
let note;
console.log(note ?? "no note");
Sample input
None
Expected output
not set 0 no note
Common mistake
Using || for defaults when 0 or an empty string is allowed.
Try this
Try quantity = null with both lines.
Source
examples/ex-009-04-zero-is-data.js

08Guided exercises

Three exercises: explore and modify, complete the implementation, then build independently. Tests check behaviour, so any correct solution passes.

Predict truthiness

Explore which values are truthy and falsy.

const values = [0, "0", "", "hello", null, 7];
console.log(Boolean(values[0]));
console.log(Boolean(values[1]));

Checkout flags

Create well-named boolean variables from comparisons.

const cartTotal = 72;
const itemsInCart = 3;
// TODO: isEmpty is true when there are no items
const isEmpty = false;
// TODO: hasFreeShipping is true when the total is at least 50
const hasFreeShipping = false;
console.log("Empty:", isEmpty, "Free shipping:", hasFreeShipping);

Profile defaults

Use ?? to supply defaults without losing valid zero values.

const savedTheme = null;
const savedFontSize = 0;      // 0 means "use the smallest size": a real choice
let savedLanguage;            // never set

09Prediction

Commit to an answer first, then run the real code to compare.

let a;
const b = null;
console.log(a);
console.log(b ?? "empty");
console.log(Boolean("false"));

What three lines are printed?

10Debugging investigation

Free samples turn into one item

A sample order has a quantity of 0 (the customer removed it). The packing list should say Quantity: 0, but it shows Quantity: 1.

const quantity = 0;
const shown = quantity || 1;
console.log("Quantity:", shown);

More cases in the Debugging Detective Laboratory

11Practical scenario

Optional form fields

Sign-up forms are full of optional values. Treating "not answered" (null), "not asked" (undefined) and "answered zero" (0) the same way leads to wrong defaults, which is why careful code uses ?? and explicit checks.

const age = 0;
console.log(age || "not given");
console.log(age ?? "not given");

12Knowledge check

1. Which value is falsy?

  • "0"
  • " "
  • 0
  • "false"

2. What does this print?

let total;
console.log(total);
  • null
  • undefined
  • 0
  • An error

3. What does this print?

console.log(typeof null);
  • "null"
  • "object"
  • "undefined"
  • "empty"

4. A quantity of 0 shows as 1. Why?

const quantity = 0;
console.log(quantity || 1);
  • 0 is not a number
  • || treats 0 as falsy and uses the default
  • 1 is the minimum quantity
  • quantity must be let

5. What does this print?

console.log(!0);
  • true
  • false
  • undefined
  • NaN

6. Which name is best for a boolean?

  • stock
  • hasStock
  • stockFlagValue
  • notOutOfStock

13Solutions and tests

Each exercise's solution appears in the exercise once you have run its tests. Solutions are one possible answer: the tests accept any implementation with the same behaviour.

Automated acceptance tests for this lesson

These are the same checks the exercises run. They are visible on purpose: client-side tests help you learn and are not secret.

  • Predict truthiness: Six truthiness results
  • Checkout flags: Prints the two flags; Flags come from comparisons
  • Profile defaults: Prints light 0 en; fontSize keeps 0; Uses ??
  • ex-009-01: output matches the documented expected output
  • ex-009-02: output matches the documented expected output
  • ex-009-03: output matches the documented expected output
  • ex-009-04: output matches the documented expected output

Download the whole lesson folder (examples, exercises, solutions, tests) as ZIP

14Source files

The lesson lives in the companion repository at curriculum/level-01-foundations/module-02-variables-values-and-types/lesson-009-booleans-null-and-undefined/. Every example on this page is generated from those files.

These links open the files in the GitHub repository (https://github.com/lsvekis/JavaScript-Professional-Engineering-Academy). You can also use the ZIP download above.

15Summary and next steps

  • Booleans are true or false; comparisons produce them.
  • undefined means not assigned; null means empty on purpose.
  • Six values are falsy: false, 0, "", null, undefined, NaN.
  • Use ?? for defaults when 0 or "" are valid values.

16AI learning prompts

Copy a prompt into the AI assistant of your choice. Then verify what it says by running code here. No AI service is connected to this page.

Concept explanation null versus undefined
Context: I mix up null and undefined.
Task: Explain the difference with a form-filling analogy and three code examples.
Constraints: Beginner level.
Expected behaviour: I can say which one a program will produce in common cases.
Testing: Give me four snippets to classify.
Code prediction Truthiness drill
Context: I know the six falsy values.
Task: Ask me ten Boolean(...) questions including tricky strings.
Constraints: Wait for my answers.
Expected behaviour: Each has one answer.
Testing: Explain each one I miss.
Debugging Default replaced my zero
Context: A setting of 0 keeps turning into the default value.
Task: Help me find the operator that causes it.
Constraints: Questions first, then a hint.
Expected behaviour: I can explain || versus ??.
Testing: Suggest a test with 0, "" and null.
Exercise generation Boolean flags
Context: I finished the checkout flags exercise.
Task: Create two exercises that build well-named booleans from comparisons.
Constraints: Fictional shop or library data, exact expected output.
Expected behaviour: Each needs at least three flags.
Testing: Include expected output.
Code review Boolean naming review
Context: I have six boolean variables in my script.
Task: Review their names and suggest yes/no question names.
Constraints: Keep the logic; rename only.
Expected behaviour: Every boolean reads as a question.
Testing: List old → new names.