01 / 21
&& || !

Logical Operators
& Short-Circuit

Master JavaScript's &&, ||, and ! operators: how they evaluate expressions, return values, and short-circuit for cleaner, faster code.

JavaScript

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.

&& AND
Returns true only when both sides are truthy
|| OR
Returns true when at least one side is truthy
! NOT
Flips a truthy value to false and vice versa

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.

Falsy (6 values)
false, 0, "", null, undefined, NaN
Truthy (everything else)
42, "hello", [], {}, "0", "false", -1

Why This Matters

Logical operators appear in every JavaScript program: conditionals, guard clauses, default values, and feature flags. Understanding them unlocks cleaner, more expressive code.

1
Conditionals: if (loggedIn && isAdmin) chains multiple checks
2
Default values: name || "Guest" provides a fallback
3
Guard clauses: user && user.save() prevents errors on null
4
Toggling: !isOpen flips a boolean flag

The && (AND) Operator

&& returns true only when both operands are truthy. If either side is falsy, the result is falsy.

A B A && B
truetruetrue
truefalsefalse
falsetruefalse
falsefalsefalse

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
truetruetrue
truefalsetrue
falsetruetrue
falsefalsefalse

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.

console.log(!true); // false console.log(!0); // true (0 is falsy) console.log(!"hello"); // false (non-empty string is truthy) console.log(!""); // true (empty string is falsy) console.log(!!"hello"); // true (double-NOT = boolean cast)

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.

&& Short-Circuit A && B If A is falsy → return A If A is truthy → return B || Short-Circuit A || B If A is truthy → return A If A is falsy → return B Key: They return an operand, not a boolean! "hello" && 42 → 42 (both truthy, returns B) "" || "default" → "default" (A falsy, returns B)

&& 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

1
Identify the operator: Is it &&, ||, or !?
2
Evaluate the left operand first. Determine if it is truthy or falsy.
3
Apply the short-circuit rule: For &&: if left is falsy, return it. For ||: if left is truthy, return it.
4
Otherwise, evaluate and return the right operand (the actual value, not a boolean coercion).
5
For chained operators, repeat left-to-right. Each result becomes the "left" for the next operator.

Example 1: Basic Evaluation

let a = 5 && "hello"; let b = 0 && "hello"; let c = "hi" || "bye"; let d = "" || "fallback"; console.log(a); // "hello" (5 is truthy → return right) console.log(b); // 0 (0 is falsy → return left) console.log(c); // "hi" ("hi" is truthy → return left) console.log(d); // "fallback" ("" is falsy → return right)

Example 2: Practical Patterns

// Pattern 1: Default values with || function greet(name) { name = name || "Guest"; return "Hello, " + name; } greet("Alice"); // "Hello, Alice" greet(""); // "Hello, Guest" (empty string is falsy!) // Pattern 2: Guard clause with && let user = null; user && user.save(); // null (user is falsy → skips .save()) let user2 = { save: () => "saved" }; user2 && user2.save(); // "saved" (user2 is truthy → runs .save())

Example 3: Chaining Operators

// Chained ||: first truthy wins let result = null || "" || 0 || "found it" || "too late"; // null → falsy, skip // "" → falsy, skip // 0 → falsy, skip // "found it" → truthy! Return "found it" console.log(result); // "found it" // Chained &&: first falsy wins let result2 = 1 && 2 && 0 && 3; // 1 → truthy, continue // 2 → truthy, continue // 0 → falsy! Return 0 console.log(result2); // 0

Operator Precedence

When !, &&, and || appear in the same expression, JavaScript follows a strict order of evaluation.

1
! (NOT): evaluated first (highest precedence)
2
&& (AND): evaluated second
3
|| (OR): evaluated last (lowest precedence)
// Without parentheses: true || false && false; // && binds tighter, so this is: true || (false && false) // = true || false = true // With parentheses for clarity: (true || false) && false; // = true && false = false

Common Mistakes

✕ Expecting Booleans
Thinking 5 && 3 returns true instead of 3. The operator returns the actual value.
✕ Falsy Zero Bug
Using count || 10 as a default: if count is 0, you lose it because 0 is falsy. Use ?? instead.
✕ Precedence Trap
Writing 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?

Question 2 of 5

What does null || "" || "default" evaluate to?

Question 3 of 5

Which statement about short-circuit evaluation is correct?

Question 4 of 5

What does !!"" evaluate to?

Question 5 of 5

What does true || false && false evaluate to?