Skip to content

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.

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); // true
console.log(149 === 200); // false
console.log('Hike'.length > 3); // true

You will use booleans constantly in if statements and other control flow — covered in Module 03.

In a boolean context (like a condition), every value in JavaScript evaluates to either truthy or falsy. There are exactly six falsy values:

false
0
'' (empty string)
null
undefined
NaN

Everything 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 means a variable has been declared but not yet assigned a value:

let tourGuide;
console.log(tourGuide); // undefined
console.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); // null
console.log(typeof tourGuide); // 'object' ← known quirk

The 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.

SituationUse
Variable declared, no value assigned yetundefined (happens automatically)
Value intentionally absent or clearednull (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.

== does type coercion before comparing. === does not — it checks value and type.

console.log(null == undefined); // true — == coerces
console.log(null === undefined); // false — === does not coerce
console.log(0 == false); // true — 0 coerces to false
console.log(0 === false); // false — number vs boolean, different types

Always 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
}
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 null
console.log(tourGuide.name); // TypeError: Cannot read properties of null

Run 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.

  • true and false are the only boolean values — keywords, no quotes.
  • The six falsy values are: false, 0, '', null, undefined, NaN. Everything else is truthy.
  • undefined means a variable was declared but never assigned. JavaScript sets it automatically — you do not assign it yourself.
  • null means intentionally empty — you set it when you want to express “no value.”
  • typeof null returns 'object' — a historical bug in the language. Just remember it.
  • Use === for equality checks. The only exception is x == null to catch both null and undefined at once.