Skip to main content

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

javascript
a && b

Returns:

  • the value of a, if a is falsy;
  • otherwise - the value of b.

How it works (step by step)

  1. JS evaluates the first expression a.
  2. If a is falsy → it immediately returns a (the rest is not evaluated).
  3. If a is truthy → it returns the second expression b.

Examples with boolean values

javascript
true && true // true true && false // false false && true // false false && false // false

Same 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.

javascript
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.

javascript
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:

javascript
isLoggedIn && showDashboard();

The call to showDashboard() happens only if isLoggedIn === true.


Example in real code

javascript
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

javascript
if (user) { console.log(user.name); } // same thing user && console.log(user.name);

&& just does the same thing more concisely.


Example in JSX (React)

javascript
{isAdmin && <AdminPanel />}

The <AdminPanel /> component renders only if isAdmin === true.


Truthy / falsy table

falsy valuestruthy values
falseeverything else
0, -0, 0n'strings'
'' (empty string)[], {}
nulltrue
undefinedany objects
NaNfunctions

Summary

ExpressionResultExplanation
true && "JS""JS"both truthy → returns the second
false && "JS"falsefirst is falsy
null && 5nullfirst is falsy
"hi" && 00second is falsy
1 && 2 && 33all truthy → returns the last
0 && 2 && 30first is falsy → stops

In short

  • a && b → if a is falsy, returns a; otherwise returns b.
  • Used for:
    • one-line condition checks,
    • short function calls,
    • safe property access,
    • JSX rendering.

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.