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 ||.
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 orfilter(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!!orBoolean(). - Choose
??over||when 0,''orfalseare valid values, and use?.to read throughnullorundefined. - Explain why
value == trueis not a truthiness test. - Key terms: truthy (treated as
truein a Boolean context); falsy (treated asfalse); Boolean context (a place that needs true/false, such asif,while,!,&&,||, ternary); short-circuit (an operator stops evaluating once the result is known); nullish (nullorundefined).
Key facts
Falsy values
- Detail
false,0,-0,0n,"",null,undefined,NaN(anddocument.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
bonly ifaisnullorundefined
obj?.prop- Detail
undefined(no throw) whenobjis nullish
if (x = 0)- Detail
- Legal; condition is the assigned value 0, so falsy
2 == true- Detail
false:truebecomes 1
arr.filter(Boolean)- Detail
- Keeps only truthy elements
Sources for this section (11)
- Falsydeveloper.mozilla.org (opens in a new tab)
- Truthydeveloper.mozilla.org (opens in a new tab)
- if...elsedeveloper.mozilla.org (opens in a new tab)
- Logical NOT (!)developer.mozilla.org (opens in a new tab)
- Logical OR (||)developer.mozilla.org (opens in a new tab)
- Logical AND (&&)developer.mozilla.org (opens in a new tab)
- Nullish coalescing operator (??)developer.mozilla.org (opens in a new tab)
- Optional chaining (?.)developer.mozilla.org (opens in a new tab)
- Assignment (=)developer.mozilla.org (opens in a new tab)
- Equality (==)developer.mozilla.org (opens in a new tab)
- Array.prototype.filter()developer.mozilla.org (opens in a new tab)
- 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