Logical Operators Return Operands, Not Booleans
You'll learn that JavaScript's logical operators don't always give you true or false—they return one of the actual values you're working with.
The Surprising Truth
Here's something that catches many beginners off guard: when you use && or ||, JavaScript doesn't convert everything to a boolean and return that. Instead, it returns one of the original operands.
Think of it like a referee who stops the game as soon as the outcome is determined, then points to whoever made the deciding move—not just raising a flag that says "yes" or "no."
How It Works
With || (OR):
- Returns the first truthy value it encounters
- If all are falsy, returns the last value
With && (AND):
- Returns the first falsy value it encounters
- If all are truthy, returns the last value
Why This Matters
This explains why patterns you've already seen actually work:
const name = username || "Guest";
This doesn't return true or false—it returns either username (if truthy) or the string "Guest".
isLoggedIn && showDashboard();
This returns either false (if isLoggedIn is falsy) or the result of calling showDashboard().
The operators are evaluating truthiness/falsiness to decide which value to return, but they return the actual operand, not a boolean.
Key Takeaway: && and || return one of the original operands based on truthiness, not true or false. This is what makes short-circuit patterns for defaults and conditional execution possible.