Logical Operators
& Short-Circuit
Master JavaScript's &&, ||, and ! operators: how they evaluate expressions, return values, and short-circuit for cleaner, faster code.
What Are Logical Operators?
Logical operators let you combine or negate boolean expressions. JavaScript has three core logical operators that control the flow of decisions in your code.
Truthy vs Falsy Values
Before mastering logical operators, you need to know which values JavaScript treats as false and which as true when coerced to a boolean.
false, 0, "", null, undefined, NaN42, "hello", [], {}, "0", "false", -1Why This Matters
Logical operators appear in every JavaScript program: conditionals, guard clauses, default values, and feature flags. Understanding them unlocks cleaner, more expressive code.
if (loggedIn && isAdmin) chains multiple checksname || "Guest" provides a fallbackuser && user.save() prevents errors on null!isOpen flips a boolean flagThe && (AND) Operator
&& returns true only when both operands are truthy. If either side is falsy, the result is falsy.
| A | B | A && B |
|---|---|---|
| true | true | true |
| true | false | false |
| false | true | false |
| false | false | false |
The || (OR) Operator
|| returns true when at least one operand is truthy. It only returns falsy when both sides are falsy.
| A | B | A || B |
|---|---|---|
| true | true | true |
| true | false | true |
| false | true | true |
| false | false | false |
The ! (NOT) Operator
The ! operator is the simplest: it negates a value. It always returns a boolean: true becomes false, and any truthy value becomes false.
What Is Short-Circuit Evaluation?
JavaScript evaluates logical expressions left to right and stops as soon as the outcome is determined. This is called short-circuit evaluation: the right operand may never run.
&& vs ||: Return Values
The most common misconception: logical operators do not always return true or false. They return one of the actual operand values.
| Expression | && returns | || returns |
|---|---|---|
| Both truthy | Second operand | First operand |
| First falsy | First (falsy) operand | Second operand |
| Second falsy | Second (falsy) operand | First operand |
| Both falsy | First (falsy) operand | Second (falsy) operand |
How to Evaluate Step by Step
&&, ||, or !?&&: if left is falsy, return it. For ||: if left is truthy, return it.Example 1: Basic Evaluation
Example 2: Practical Patterns
Example 3: Chaining Operators
Operator Precedence
When !, &&, and || appear in the same expression, JavaScript follows a strict order of evaluation.
! (NOT): evaluated first (highest precedence)&& (AND): evaluated second|| (OR): evaluated last (lowest precedence)Common Mistakes
5 && 3 returns true instead of 3. The operator returns the actual value.count || 10 as a default: if count is 0, you lose it because 0 is falsy. Use ?? instead.a || b && c and expecting left-to-right grouping. Remember: && binds tighter than ||.Summary Cheat Sheet
| Operator | Short-Circuits When | Returns | Common Use |
|---|---|---|---|
&& |
Left is falsy | First falsy or last value | Guard clauses |
|| |
Left is truthy | First truthy or last value | Default values |
! |
N/A (unary) | Always a boolean | Toggling, checks |
!! |
N/A (double unary) | Always a boolean | Boolean conversion |
Question 1 of 5
What does the expression "hello" && 0 evaluate to?
"hello" is truthy, so it continues to the right operand and returns 0 (the actual value, not false).Question 2 of 5
What does null || "" || "default" evaluate to?
null is falsy (skip), "" is falsy (skip), "default" is truthy: return "default".Question 3 of 5
Which statement about short-circuit evaluation is correct?
Question 4 of 5
What does !!"" evaluate to?
"" is falsy. !"" is true. !!"" flips again to false. Double-NOT converts a value to its boolean equivalent.Question 5 of 5
What does true || false && false evaluate to?
true || (false && false). The inner && evaluates to false. Then true || false evaluates to true.