CCA Prep

Lesson 1.4Lesson 4 of 24

Truthy and falsy values

Given a specific scenario, distinguish truthy or falsey evaluations.

Truthy and falsy valuesFalsyfalse, 0, -0, 0nempty stringnull, undefinedNaNTruthy, often by surprise'0' and 'false'[] and {}new Boolean(false)a string with one space

Key points

  1. 1Only these are falsy: false, 0, -0, 0n, the empty string, null, undefined and NaN. Everything else is truthy.
  2. 2Every object is truthy, including [], {} and new Boolean(false). Test an empty array with its length.
  3. 3|| and && return one of their operands, not a boolean: || the first truthy one, && the first falsy one, or else the last operand.
  4. 4?? falls back only for null and undefined, so it keeps 0 and the empty string where || would replace them.
  5. 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