Booleans, null, and undefined
Not every value is a number or a string. JavaScript has three more primitive types you will encounter constantly: booleans, null, and undefined. Knowing how they behave prevents a category of bugs that confuse beginners for weeks.
Booleans
Section titled “Booleans”A boolean has exactly two possible values: true or false. No quotes, no capitalization — they are keywords.
let tourAvailable = true;let isBookingOpen = false;Booleans are the natural result of comparisons and conditions:
console.log(149 > 100); // trueconsole.log(149 === 200); // falseconsole.log('Hike'.length > 3); // trueYou will use booleans constantly in if statements and other control flow — covered in Module 03.
Truthy and falsy
Section titled “Truthy and falsy”In a boolean context (like a condition), every value in JavaScript evaluates to either truthy or falsy. There are exactly six falsy values:
false0'' (empty string)nullundefinedNaNEverything else is truthy — including '0', [], and {}.
This means you can use any value in a condition, not just an explicit true or false:
const tourName = 'Cascade Ridge Hike';if (tourName) { console.log('We have a tour name.'); // this runs — non-empty string is truthy}
const emptyName = '';if (emptyName) { console.log('This will not run.'); // empty string is falsy}Truthy/falsy is a feature, not a trap — once you know the six falsy values, you can use it deliberately.
undefined
Section titled “undefined”undefined means a variable has been declared but not yet assigned a value:
let tourGuide;console.log(tourGuide); // undefinedconsole.log(typeof tourGuide); // 'undefined'You can also get undefined from accessing an object property that does not exist, or from a function that does not return a value. Think of undefined as “this exists, but nothing has been put here yet.”
null represents an intentionally empty value — a deliberate absence, not an oversight:
let tourGuide = null;console.log(tourGuide); // nullconsole.log(typeof tourGuide); // 'object' ← known quirkThe typeof null === 'object' result is a bug in the original JavaScript specification that has never been fixed because fixing it would break existing code. It is one of the language’s known oddities — just remember it.
Use null when you intentionally want a variable to have no value: a feature that is not yet loaded, a selected item that has been cleared, a field the user has not filled in.
null vs undefined: when to use which
Section titled “null vs undefined: when to use which”| Situation | Use |
|---|---|
| Variable declared, no value assigned yet | undefined (happens automatically) |
| Value intentionally absent or cleared | null (you set it explicitly) |
In practice: you never assign undefined yourself. JavaScript sets it automatically. When you want to express “empty on purpose,” use null.
Equality: == vs ===
Section titled “Equality: == vs ===”== does type coercion before comparing. === does not — it checks value and type.
console.log(null == undefined); // true — == coercesconsole.log(null === undefined); // false — === does not coerce
console.log(0 == false); // true — 0 coerces to falseconsole.log(0 === false); // false — number vs boolean, different typesAlways use === unless you have a specific reason to allow coercion. The only common exception is x == null, which is a widely-used shorthand that catches both null and undefined:
if (tourGuide == null) { console.log('No guide assigned.'); // catches both null and undefined}Exercise
Section titled “Exercise”const tourAvailable = true;const tourName = 'Cascade Ridge Hike';const tourPrice = 149;let tourGuide = null;
console.log(typeof tourAvailable); // 'boolean'console.log(typeof tourName); // 'string'console.log(typeof tourPrice); // 'number'console.log(typeof tourGuide); // 'object' — the null quirk
// Observe the TypeError when accessing a property of nullconsole.log(tourGuide.name); // TypeError: Cannot read properties of nullRun the last line, read the error message in the console, then comment it out. This is the exact error you will see when code tries to use a DOM element that does not exist yet.
trueandfalseare the only boolean values — keywords, no quotes.- The six falsy values are:
false,0,'',null,undefined,NaN. Everything else is truthy. undefinedmeans a variable was declared but never assigned. JavaScript sets it automatically — you do not assign it yourself.nullmeans intentionally empty — you set it when you want to express “no value.”typeof nullreturns'object'— a historical bug in the language. Just remember it.- Use
===for equality checks. The only exception isx == nullto catch bothnullandundefinedat once.