Tutorials JavaScript Logical Operators
Ch 15 / 78
Chapter 15 Beginner 7 min read JavaScript

Logical Operators

Logical operators combine or negate boolean expressions and return a value based on their operands' truthiness. JavaScript's &&, ||, and ! are the foundation of every conditional decision — and thanks to short-circuit evaluation, they're also powerful tools for writing concise, expressive code far beyond simple true/false logic.

What you'll learn
The 4 logical operators: &&, ||, !, ??
Short-circuit evaluation in && and ||
Nullish coalescing (??) vs logical OR (||)
Logical assignment: &&=, ||=, ??=
Truthy and falsy values in logical context
Real-world patterns: default values, guard clauses

Logical Operator Overview

JavaScript provides four logical operators. Unlike in many languages, && and || do not simply return true or false — they return one of their operands. This makes them extremely versatile for writing concise expressions.

AND (&&)
Returns first falsy operand, or the last value if all are truthy.
OR (||)
Returns first truthy operand, or the last value if all are falsy.
NOT (!)
Negates a boolean. Always returns true or false.
Nullish (??)
Returns right side only when left is null or undefined.
Operator Name Example Result
&& Logical AND true && false false
|| Logical OR false || true true
! Logical NOT !true false
?? Nullish Coalescing null ?? "default" "default"
&&= Logical AND Assignment a &&= b a = a && b
||= Logical OR Assignment a ||= b a = a || b
??= Nullish Assignment a ??= b a = a ?? b

AND (&&) and OR (||)

The && and || operators evaluate their operands based on truthiness. Crucially, they return one of their original operands — not necessarily a boolean. This is what makes short-circuit evaluation so powerful in JavaScript.

&&
AND — stops at first falsy
true && true true
true && false false
false && true false
1 && "hello" "hello"
0 && "hello" 0
null && "ok" null
||
OR — stops at first truthy
true || false true
false || true true
false || false false
0 || "hello" "hello"
"hi" || "bye" "hi"
null || undefined undefined
JavaScript — && and || Operators
// ── AND (&&) — returns first falsy OR last value ──────────────
true && true; // true
true && false; // false
1 && "hello"; // "hello" — 1 is truthy, returns second operand
0 && "hello"; // 0 — 0 is falsy, short-circuits here
null && "ok"; // null — null is falsy, returns null

// ── OR (||) — returns first truthy OR last value ─────────────
false || true; // true
0 || "hello"; // "hello" — 0 is falsy, tries next
"hi" || "bye"; // "hi" — "hi" is truthy, returns immediately
null || undefined; // undefined — both falsy, returns last

// ── Practical use: default values ────────────────────────────
const name = userInput || "Guest"; // fallback if userInput is falsy
const port = config.port || 3000; // default port if not set
Key Insight
&& and || don't return true or false — they return one of their operands. This is why 0 || "fallback" returns "fallback" (a string), not true. Always keep this in mind when using them for default values.

NOT Operator (!)

The ! operator negates a value's truthiness. Unlike && and ||, it always returns a strict boolean — either true or false. The double-NOT !! is a common idiom to convert any value to its boolean equivalent.

!
Single NOT
Converts value to boolean and flips it.
!true → false
!!
Double NOT
Converts any value to its boolean equivalent.
!!"hello" → true
!0
Falsy Negation
Falsy values become true when negated.
!0 → true
![]
Object Negation
Objects and arrays are always truthy — even empty ones.
![] → false
JavaScript — NOT Operator
// ── Single NOT (!) ───────────────────────────────────────────
!true; // false
!false; // true
!0; // true — 0 is falsy
!""; // true — empty string is falsy
!null; // true — null is falsy
!"hello"; // false — non-empty string is truthy
![1, 2, 3]; // false — arrays are always truthy
!{}; // false — objects are always truthy

// ── Double NOT (!!) — coerce to boolean ─────────────────────
!!0; // false
!!""; // false
!!null; // false
!!"hello"; // true
!!42; // true
!![]; // true

// ── Equivalent to Boolean() ──────────────────────────────────
Boolean(0); // false — same as !!0
Boolean("hi"); // true — same as !!"hi"

Short-Circuit Evaluation

Short-circuit evaluation means JavaScript stops evaluating a logical expression as soon as the result is determined. With &&, if the left side is falsy, the right side is never evaluated. With ||, if the left side is truthy, the right side is skipped. This has real performance and safety implications.

&&
AND Short-Circuit
If left is falsy, right side is skipped entirely — even if it has side effects.
user && user.getName()
||
OR Short-Circuit
If left is truthy, right side is skipped — great for default value patterns.
name || "Anonymous"
JavaScript — Short-Circuit Evaluation
// ── && guard pattern — safe property/method access ───────────
const user = null;
user && user.getName(); // null — getName() never called (safe!)

const loggedIn = true;
loggedIn && showDashboard(); // showDashboard() IS called

// ── || default value pattern ─────────────────────────────────
const theme = null;
const activeTheme = theme || "dark"; // "dark"

const count = 0;
const display = count || "No items"; // ⚠ "No items" — 0 is falsy!

// ── Avoid side-effect bugs ───────────────────────────────────
let x = 0;
false && x++; // x is still 0 — right side never runs
true || x++; // x is still 0 — right side short-circuited

// ── Conditional rendering (JSX-like pattern) ─────────────────
const isAdmin = true;
isAdmin && renderAdminPanel(); // renders panel only when true
Watch Out
When using || for default values, remember that all falsy values trigger the fallback — including 0, "", and false. If your variable could legitimately be 0 or an empty string, use ?? (nullish coalescing) instead.

Nullish Coalescing (??)

Introduced in ES2020, the ?? operator is similar to || but with a crucial difference: it only returns the right-hand side when the left side is null or undefined — not for 0, "", or false.

Left Operand a || "default" a ?? "default"
null "default" "default"
undefined "default" "default"
0 "default" 0
"" "default" ""
false "default" false
"hello" "hello" "hello"
JavaScript — Nullish Coalescing (??)
// ── ?? only triggers on null / undefined ─────────────────────
null ?? "default"; // "default"
undefined ?? "default"; // "default"
0 ?? "default"; // 0 — 0 is NOT null/undefined!
"" ?? "default"; // "" — empty string passes through
false ?? "default"; // false — false passes through

// ── Real-world: config with zero as valid value ───────────────
const volume = userSettings.volume ?? 50;
// With ||: volume=0 would be replaced by 50 (wrong!)
// With ??: volume=0 is preserved correctly ✓

// ── Chaining ?? ──────────────────────────────────────────────
const name = firstName ?? lastName ?? "Anonymous";

// ── Optional chaining + ?? (powerful combo) ──────────────────
const city = user?.address?.city ?? "Unknown";

Logical Assignment Operators

ES2021 introduced three logical assignment operators that combine a logical operation with assignment. They only assign the value if the assignment would actually change anything — making them very concise and efficient for setting defaults or updating values conditionally.

&&=
AND Assignment
Assigns right side only if left side is truthy.
a &&= b → a = a && b
||=
OR Assignment
Assigns right side only if left side is falsy.
a ||= b → a = a || b
??=
Nullish Assignment
Assigns right side only if left is null/undefined.
a ??= b → a = a ?? b
JavaScript — Logical Assignment Operators
// ── &&= : assign only if truthy ───────────────────────────────
let a = 1;
a &&= 99; // a = 99 — a was truthy
let b = 0;
b &&= 99; // b = 0 — b was falsy, no assignment

// ── ||= : assign only if falsy ────────────────────────────────
let name = "";
name ||= "Guest"; // name = "Guest" — was falsy
let user = "Alice";
user ||= "Guest"; // user = "Alice" — was truthy, not changed

// ── ??= : assign only if null/undefined ──────────────────────
let count = null;
count ??= 0; // count = 0 — was null
let score = 0;
score ??= 100; // score = 0 — 0 is NOT null/undefined

// ── Real-world: initialise object properties ─────────────────
const settings = {};
settings.theme ??= "dark"; // sets default if missing
settings.fontSize ??= 16; // sets default if missing

Full Truth Table

The complete truth table for all combinations of &&, ||, and ! with boolean operands.

A B A && B A || B !A !B
true true true true false false
true false false true false true
false true false true true false
false false false false true true

Try It — Live Editor

Experiment with JavaScript logical operators below. Modify operands, switch between &&, ||, and ??, and observe short-circuit evaluation live — all without leaving this page.

Live Editor
✎ Editor
👁 Preview

Common Patterns & Recipes

These logical operator patterns appear constantly in real JavaScript projects. Master them to write clean, concise, and bug-free code.

JavaScript — Logical Operator Recipes
// ── Safe property access (guard clause) ─────────────────────
const city = user && user.address && user.address.city;
// Or with optional chaining (modern):
const city2 = user?.address?.city;

// ── Default value with ?? (correct for 0 / "") ───────────────
const timeout = options.timeout ?? 5000;
const label = props.label ?? "Untitled";

// ── Conditional function call ─────────────────────────────────
isLoggedIn && renderDashboard();
hasError && console.error(error);

// ── Toggle boolean ───────────────────────────────────────────
let isOpen = false;
isOpen = !isOpen; // true → false → true …

// ── Combine conditions ───────────────────────────────────────
const canEdit = isLoggedIn && (isAdmin || isOwner);
const showBanner = !hasSeenBanner && !isLoading;

// ── Default with OR vs ?? (know the difference!) ─────────────
const retries = options.retries || 3; // ⚠ 0 becomes 3
const retries2 = options.retries ?? 3; // ✓ 0 stays 0

// ── Initialise config properties ─────────────────────────────
const cfg = {};
cfg.debug ??= false;
cfg.timeout ??= 3000;
cfg.retries ??= 3;
⚡
Free Resource
Download our JavaScript Cheat Sheet
All operators, logical patterns & short-circuit rules — one printable page.
Get Sheet
Quick Check
What does 0 || "fallback" evaluate to?
Quick Check
What does 0 ?? "fallback" evaluate to?
Quick Check
What is the result of null && "hello"?
Chapter Summary
  • JavaScript has four logical operators: && (AND), || (OR), ! (NOT), and ?? (Nullish Coalescing). ES2021 added &&=, ||=, and ??=.
  • && returns the first falsy operand, or the last value if all are truthy. It stops evaluating as soon as a falsy value is found (short-circuit).
  • || returns the first truthy operand, or the last value if all are falsy. Useful for default values, but triggers on 0, "", and false too.
  • ! always returns a strict boolean. Double-not (!!) converts any value to its boolean equivalent — same as Boolean(value).
  • The nullish coalescing operator ?? only falls back when the left side is null or undefined — not for 0, "", or false. Prefer it over || for default values.
  • Short-circuit evaluation means the right-hand side of && is skipped when the left is falsy, and the right of || is skipped when the left is truthy. Be careful with side effects inside these expressions.
  • Logical assignment operators (&&=, ||=, ??=) only perform the assignment if the logical condition requires it — great for setting defaults without overwriting existing values.

Rate Us on Google

4.8 / 5
Enjoying this tutorial?
Your 5-star review helps other learners discover our free tutorials and encourages us to keep creating quality content.
80+ reviews
Write a Google Review
Join WhatsApp Channel