Lesson 1.4Lesson 4 of 24
Truthy and falsy values
Given a specific scenario, distinguish truthy or falsey evaluations.
Key points
- 1Only these are falsy: false, 0, -0, 0n, the empty string, null, undefined and NaN. Everything else is truthy.
- 2Every object is truthy, including [], {} and new Boolean(false). Test an empty array with its length.
- 3|| and && return one of their operands, not a boolean: || the first truthy one, && the first falsy one, or else the last operand.
- 4?? falls back only for null and undefined, so it keeps 0 and the empty string where || would replace them.
- 5Optional chaining (?.) stops at null or undefined and returns undefined for the whole rest of the chain.
What the exam guide checks here
Objective wording from the exam guide (English).
- 1.4.K1The falsy values; everything else, including [] and {}, is truthy
- 1.4.K2Logical operators return operands: ||, &&, ??, and optional chaining
- 1.4.S1Predict which branch runs or which value a logical expression returns
Lock it in
Answer scenario questions on this topic. Every option comes with an explanation.
Practise 1.4 · 12 questions