Boolean Coercion in Logical Operators
What you'll learn: How && and || don't actually coerce values to booleans—they return one of the original operands instead.
The Surprise About Logical Operators
You might think && and || convert everything to true or false, but they don't! Instead, they evaluate the truthiness or falsiness of operands and then return one of the actual operands, not a boolean.
How It Really Works
With || (OR):
0 || 'default' // returns 'default' (not true!)
'' || 100 // returns 100
false || 42 // returns 42
The || operator checks if the left side is falsy. If it is, it returns the right side. If the left is truthy, it returns the left side.
With && (AND):
'hello' && 5 // returns 5
0 && 'default' // returns 0 (not false!)
true && null // returns null
The && operator checks if the left side is truthy. If it is, it returns the right side. If the left is falsy, it returns the left side.
Why This Matters
This is why patterns like 0 || 'default' work for providing fallback values—but be careful with 0, empty strings, or null as legitimate values:
let count = 0;
let display = count || 'No items'; // 'No items' (oops! 0 is falsy)
The operators perform boolean evaluation internally (checking truthiness), but they return the original operands, not coerced booleans.
Key Takeaway: && and || return one of their operands based on truthiness checks, not true/false, which is why 0 || 'default' returns 'default', not a boolean.