The && (AND) Operator
You'll learn how the && operator evaluates two values and returns the first falsy value it finds—or the last value if all are truthy.
What Does && Do?
The && (AND) operator checks both operands from left to right. If the first value is falsy, it immediately returns that first value without even looking at the second. If the first value is truthy, it returns the second value (whatever it is).
Think of it like a gatekeeper: if the first person fails the test, they're sent back immediately. If they pass, you get whatever the second person brings.
How It Works
// Returns first falsy value
false && true; // false
0 && 42; // 0
"" && "hello"; // ""
null && "anything"; // null
// Returns last value if all truthy
true && true; // true
5 && 10; // 10
"hi" && "bye"; // "bye"
1 && 2 && 3; // 3
Notice how && doesn't always return true or false—it returns one of the actual operands.
Short-Circuit Evaluation
If the first operand is falsy, JavaScript doesn't even evaluate the second operand. This is called "short-circuiting":
false && someFunction(); // someFunction never runs
Practical Use
let age = 25;
let hasID = true;
// Only returns true if BOTH are truthy
age >= 21 && hasID; // true
let age2 = 18;
age2 >= 21 && hasID; // false (first condition fails)
Key Takeaway: The && operator returns the first falsy value it encounters, or the last value if all operands are truthy—and it stops evaluating as soon as it finds a falsy value.