The && operator
The && operator in JavaScript is logical AND.
It is used not only for logical comparisons but also for controlling the execution of expressions (short-circuit evaluation).
Basic meaning
a && bReturns:
- the value of
a, ifais falsy; - otherwise - the value of
b.
How it works (step by step)
- JS evaluates the first expression
a. - If
ais falsy → it immediately returnsa(the rest is not evaluated). - If
ais truthy → it returns the second expressionb.
Examples with boolean values
true && true // true
true && false // false
false && true // false
false && false // falseSame as in logic:
A ∧ B = "true if both are true".
Examples with other types
JS does not coerce values to
true/false, it returns one of the expressions themselves.
console.log(1 && 2); // 2 (both truthy, returns the second)
console.log(0 && 2); // 0 (first is falsy)
console.log('hi' && 123); // 123
console.log(null && 'ok'); // null
console.log(true && 'JS'); // "JS"Short-circuit
If the first value is falsy, the second is not even evaluated.
false && console.log('Will not run');
0 && console.log('Will not run');
null && console.log('Will not run');This lets you use
&&for conditional execution:
isLoggedIn && showDashboard();The call to showDashboard() happens only if isLoggedIn === true.
Example in real code
const user = { name: 'Tim' };
console.log(user && user.name); // "Tim"
const guest = null;
console.log(guest && guest.name); // null (no error!)This is handy for safe property access (before
?.- optional chaining - existed).
Difference from if
if (user) {
console.log(user.name);
}
// same thing
user && console.log(user.name);
&&just does the same thing more concisely.
Example in JSX (React)
{isAdmin && <AdminPanel />}The
<AdminPanel />component renders only ifisAdmin === true.
Truthy / falsy table
| falsy values | truthy values |
|---|---|
false | everything else |
0, -0, 0n | 'strings' |
'' (empty string) | [], {} |
null | true |
undefined | any objects |
NaN | functions |
Summary
| Expression | Result | Explanation |
|---|---|---|
true && "JS" | "JS" | both truthy → returns the second |
false && "JS" | false | first is falsy |
null && 5 | null | first is falsy |
"hi" && 0 | 0 | second is falsy |
1 && 2 && 3 | 3 | all truthy → returns the last |
0 && 2 && 3 | 0 | first is falsy → stops |
In short
a && b→ ifais falsy, returnsa; otherwise returnsb.- Used for:
- one-line condition checks,
- short function calls,
- safe property access,
- JSX rendering.
Short Answer
Interview readyA concise answer to help you respond confidently on this topic during an interview.