Trap #1: Logical OR (||) Overwriting Legitimate Falsy Values
Using val || fallback treats 0, false, and "" as missing values and replaces them with the fallback. Use the nullish coalescing operator val ?? fallback instead.
Trap #2: Empty Arrays and Objects Evaluating to Truthy
In JavaScript, Boolean([]) and Boolean({}) are both true! Checking if (items) does not test whether an array has elements; check items.length > 0.
Trap #3: Loose Equality (==) Type Coercion Hazards
The loose equality operator performs bizarre type coercions: "" == 0 is true, 0 == "0" is true, but "" == "0" is false! Always enforce strict equality ===.
Trap #4: new Boolean(false) Object Wrapper Trap
Invoking new Boolean(false) creates an object wrapper. Because all objects are truthy, if (new Boolean(false)) executes the branch! Use Boolean(val) without new.
Trap #5: Short-Circuiting in React/JSX Rendering 0
Writing count && <Badge /> in JSX renders 0 directly onto the webpage when count is 0. Use count > 0 && <Badge /> or a ternary.
💬 Frequently Asked Questions
What is the double exclamation mark (!!) idiom?
!! is a shorthand that casts any value to its boolean primitive equivalent (equivalent to Boolean(val)). The first ! negates and coerces, and the second ! restores correct polarity.
What values are strictly falsy in JavaScript?
There are exactly 8 falsy values: false, 0, -0, 0n (BigInt zero), "" (empty string), null, undefined, and NaN.
How does the nullish coalescing operator (??) work?
a ?? b evaluates and returns a unless a is strictly null or undefined, in which case it returns b, safely preserving 0 and false.
What is short-circuit evaluation?
In logical operations, if the outcome is guaranteed by the first operand, subsequent operands are not evaluated (e.g. false && expensiveFunc() skips the function).
Why should strict equality (===) be used everywhere?
Strict equality verifies both value and type without performing implicit type coercion, preventing unpredictable runtime bugs and logic failures.