Variables, Types, and Collections23% of the exam

Truthy and falsy evaluations

The eight falsy values and why everything else (including '0', 'false', [] and {}) is truthy; how !, !!, Boolean(), && and || behave and what they return; and when ?? and ?. are the right defaults instead of ||.

Official exam objective
Given a specific scenario, distinguish truthy or falsey evaluations.

~5 min read · 10 practice questions ·

What the exam expects

  • Name every falsy value and recognise look-alikes that are truthy ('0', 'false', ' ', [], {}, new Boolean(false)).
  • Predict which branch an if, ternary or filter(Boolean) takes, including when the condition is an accidental assignment.
  • Predict the value (not just the truthiness) returned by && and ||, and convert to a real boolean with !! or Boolean().
  • Choose ?? over || when 0, '' or false are valid values, and use ?. to read through null or undefined.
  • Explain why value == true is not a truthiness test.
  • Key terms: truthy (treated as true in a Boolean context); falsy (treated as false); Boolean context (a place that needs true/false, such as if, while, !, &&, ||, ternary); short-circuit (an operator stops evaluating once the result is known); nullish (null or undefined).

Key facts

Falsy values
Detail
false, 0, -0, 0n, "", null, undefined, NaN (and document.all)
[], {}, '0', 'false'
Detail
Truthy
new Boolean(false)
Detail
Truthy (an object)
!!x / Boolean(x)
Detail
Converts to a boolean primitive
a || b
Detail
First truthy operand, else the last
a && b
Detail
First falsy operand, else the last
a ?? b
Detail
b only if a is null or undefined
obj?.prop
Detail
undefined (no throw) when obj is nullish
if (x = 0)
Detail
Legal; condition is the assigned value 0, so falsy
2 == true
Detail
false: true becomes 1
arr.filter(Boolean)
Detail
Keeps only truthy elements
Sources for this section (11)
  • Why does `[0, '0', [], ''].filter(Boolean)` keep `'0'` and `[]`?
  • When should you use `??` instead of `||` for a default?
  • What does `'admin' && '' && 'user'` evaluate to?
  • Why does `if (retriesLeft = 0)` run the `else` branch?
  • Which value does `Boolean(new Boolean(false))` return?

The questions are free to read. Answers with their sources and self-grading are included with the pack.

Keep reading with the JavaScript Developer pack

  • Core concepts
  • Exam traps
  • Worked scenario

The pack includes:

  • All 24 lessons of the JavaScript Developer guide, with reading progress
  • Interactive decision walks and step-by-step sequence steppers
  • Inline practice questions and one-click topic practice (10 questions for this lesson)
  • An official source cited for every claim
  • An AI tutor that answers from the guide
  • Unlimited timed practice exams

Practice for the JavaScript Developer exam