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?"
typeof undefined is "undefined" but typeof null is "object" for historical reasons. Check for null with value === null, and for either with value == null (one of the few accepted uses of loose equality).
Truthiness is a convenience with traps: 0 and "" are often valid data. Prefer explicit checks (count === 0, text.length === 0) or the nullish coalescing operator ??, which only falls back on null or undefined.
APIs differ: missing object properties read as undefined, DOM lookups return null, JSON has null but no undefined (undefined properties are dropped by JSON.stringify).
Name booleans as yes/no questions: isValid, hasStock, canCheckout. Avoid double negatives such as isNotDisabled.
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.
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.
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.
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
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.
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 explanationnull 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 predictionTruthiness 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.
DebuggingDefault 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 generationBoolean 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 reviewBoolean 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.