JavaScript Logical AND, OR and NOT: Short-Circuiting
Quick answer
Logical AND && and OR || choose an operand value based on truthiness, sometimes skipping the right operand. NOT ! converts truthiness to its opposite Boolean.
How it works
a && b returns a when a is falsy; otherwise it evaluates and returns b. a || b returns a when a is truthy; otherwise it evaluates and returns b. This conditional evaluation is short-circuiting. !a always returns a Boolean. && binds more tightly than ||; use grouping to clarify compound decisions.
const age = 20;const hasTicket = true;console.log(age >= 18 && hasTicket);console.log(!hasTicket);Expected output in Node.js:
truefalseBoth requirements hold; NOT flips the ticket flag.
let attempts = 0;console.log("ready" || ++attempts);console.log(false && ++attempts);console.log(attempts);Expected output in Node.js:
readyfalse0Both right-side updates are skipped, so attempts remains zero.
Common mistakes
AND and OR do not necessarily return true or false. A truthy default can replace legitimate 0 or empty text. Use a nullish default when only null and undefined mean missing.
Practice
Return whether both an age requirement and a ticket flag hold.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
JavaScript Logical AND, OR and NOT: Short-Circuiting — Knowledge Check
Apply the taught concept and reason about the result.
What is 0 && 9?
What is "ok" || "fallback"?
What is !"false"?
With let n=0; true || ++n; what is n?
How does && differ from &?