Typorithm · medium

Truthy & Falsy

truthy-falsyinterview-questionstruthyfalsyboolean-coercion
Saved on this device
Full lesson · 8 learning steps Open any exercise for focused practice
1

Step 1 of 8 · intro card

Learn Truthy & Falsy

0%

JS Gotchas

Exactly seven values are falsy: false, 0, "", null, undefined, NaN, and 0n. Everything else is truthy. Boolean(x) and !!x coerce to a boolean. Watch out: a || b drops valid 0 and "", while a ?? b keeps them.

There are exactly seven falsy values: false, 0, "", null, undefined, NaN, and 0n (the BigInt zero). Every other value — including "0", [], and {} — is truthy.

Boolean(x) returns x's truthiness, and !!x is the idiomatic shorthand: the first ! coerces-and-negates, the second negates back to the real boolean.

The classic pitfall is a || b versus a ?? b: || treats 0 and "" as falsy and replaces them, whereas ?? only replaces null and undefined — so use ?? when 0 or "" are valid inputs.

01

Seven falsy values: false, 0, "", null, undefined, NaN, 0n.

02

Boolean(x) and !!x coerce to a boolean.

03

|| drops 0 and ""; ?? keeps them.

// the 7 falsy values
false, 0, "", null, undefined, NaN, 0n
Boolean("") // false
!!"hi"      // true — double-negate to coerce
0 || "x"    // "x"  — || treats 0 as falsy
0 ?? "x"    // 0    — ?? keeps the 0

Read the implementation

Connect each part of the code to the concept before you type.

1. The 7 falsy values

Exactly seven values are falsy: false, 0, "", null, undefined, NaN, and 0n. Everything else is truthy.

false, 0, "", null, undefined, NaN, 0n

2. Boolean coercion

Boolean(x) returns the truthiness of x; an empty string is falsy, so it yields false.

Boolean("") // false

3. Double negation

!!x is shorthand for Boolean(x): one ! coerces and negates, the second negates back.

!!"hi"      // true — double-negate to coerce

4. || and 0

Because 0 is falsy, || skips it and returns the right operand, which is a common bug.

0 || "x"    // "x"  — || treats 0 as falsy

5. ?? keeps 0

?? only replaces null or undefined, so a valid 0 is preserved.

0 ?? "x"    // 0    — ?? keeps the 0

A complete learning loop

01

Understand

Read the concept map and follow the code breakdown.

02

Reconstruct

Type the implementation, repair mistakes and work through every recall exercise.

03

Verify

Use the final check without hints; revisit weak concepts in short practice sessions.

Truthy & Falsy: Lesson & Practice · Typorithm | ApplyDjinn