Learn JavaScript
❯ operators.op_logical

Logical Operators (&&, ||, !) in JavaScript

$concept

JavaScript logical operators return operand values, not booleans: a && b yields a if falsy else b; a || b yields a if truthy else b. This powers idioms like config = opts || defaults and render = ready && <UI/> β€” but also the classic 0/"" clobbering that ?? now fixes.

Both short-circuit (right side may never evaluate β€” the guard pattern fn && fn()), ! always yields real boolean, and !! normalizes. Precedence (!, then &&, then ||) matches C, so the same parenthesization habits apply.

$how it works
1

Values, not booleans

"hi" && 42 is 42; 0 || "d" is "d" β€” the operators select operands by truthiness.

2

Short-circuit guards

user && user.name never touches .name when user is nullish β€” evaluation stops at the decided operand.

3

The 0/"" trap

count || 10 and name || "anon" clobber legitimate 0/"" β€” ?? exists precisely to default only null/undefined.

4

! normalizes

!value is always boolean; !!value is the standard truthiness-to-boolean probe.

$example
console.log("hi" && 42, 0 && 42);
console.log(0 || "d", "" || "d");
Output
42 0
d d
1&& returns the second operand when the first is truthy (42), the first when falsy (0).
2|| returns the first truthy operand β€” both falsy inputs fall through to "d".
3Value-returning logic enables the guard and default idioms throughout real code.
$use cases
Guard expressions (user && user.name) before ?. existed β€” and still
Conditional rendering (ready && <Panel/>) in React
Recognizing ||-defaulting as a 0/"" bug source during review
Normalizing with !! at boolean boundaries

Recommended resourcesAffiliate links Β· may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice β€” pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from β€” great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in JavaScript yourself β€” building beats watching.