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.
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.
| 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 (&&) — 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
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 (!) ───────────────────────────────────────────
!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.
// ── && 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|| 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" |
// ── ?? 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.
// ── &&= : 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.
Common Patterns & Recipes
These logical operator patterns appear constantly in real JavaScript projects. Master them to write clean, concise, and bug-free code.
// ── 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;0 || "fallback" evaluate to?0 ?? "fallback" evaluate to?null && "hello"?- 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 on0,"", andfalsetoo.!always returns a strict boolean. Double-not (!!) converts any value to its boolean equivalent — same asBoolean(value).- The nullish coalescing operator
??only falls back when the left side isnullorundefined— not for0,"", orfalse. 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
Join Our Learning Community
Connect with thousands of learners, get daily tips, ask questions and stay updated with new tutorials.