Logical Operators
And, or and not let you combine several conditions into one, and the first two return a value rather than just a boolean.
&& needs both sides, || needs either, ! flips. Both && and || short-circuit, stopping as soon as the answer is known, and they return one of the operands rather than true or false. ?? fills in only for null and undefined.
EXERCISE
1Combining Conditions with And, Or and Not
Real decisions usually depend on more than one thing, and three operators cover every combination you will need.
&& is true only when both sides are true:
const age = 25;
const hasLicence = true;
console.log(age >= 18 && hasLicence); // true
console.log(age >= 65 && hasLicence); // false
// Read it as "and": old enough AND has a licence.
|| is true when either side is true:
const isAdmin = false;
const isOwner = true;
console.log(isAdmin || isOwner); // true
console.log(isAdmin || false); // false
// Read it as "or": can edit if admin OR owner.
! flips a boolean:
const isLoggedOut = true;
console.log(!isLoggedOut); // false
const name = "";
console.log(!name); // true "" is falsy, so ! makes it true
console.log(!!name); // false two flips = "convert to boolean"
Combining them, with brackets for clarity:
const role = "editor";
const isBanned = false;
const canPublish = (role === "editor" || role === "admin") && !isBanned;
console.log(canPublish); // true
// && binds tighter than ||, so without brackets this would mean
// something different. Add the brackets even when they are optional.
Pro Tip:
!!valuelooks like a typo and is not. It is the shortest way to convert anything to a real boolean, because the first!gives you the opposite as a boolean and the second flips it back. You will see it in real code, andBoolean(value)says the same thing more clearly.
EXERCISE
2Short-Circuiting, and What These Operators Return
And and or stop evaluating the moment the answer is settled, and what they hand back is not always true or false.
This is the part that surprises people: && and || return one of the values you gave them, not a boolean.
What they actually return:
console.log(1 && 2); // 2 both truthy, so the last one
console.log(0 && 2); // 0 first is falsy, stop there
console.log("a" || "b"); // "a" first is truthy, stop there
console.log("" || "b"); // "b" first is falsy, so the next
// The rule:
// && returns the first FALSY value, or the last value.
// || returns the first TRUTHY value, or the last value.
Short-circuiting means the right side may never run:
function noisy() {
console.log("I ran");
return true;
}
false && noisy(); // nothing prints: && already knows it is false
true || noisy(); // nothing prints: || already knows it is true
true && noisy(); // "I ran"
Which is why this pattern is everywhere:
const user = null;
// Guard: only reach for .name if user exists
console.log(user && user.name); // null, and no crash
// Default: use "Guest" when name is missing
const name = "" || "Guest";
console.log(name); // Guest
// Conditional call, common in React and event handlers:
const onSave = null;
onSave && onSave(); // does nothing, does not throw
Pro Tip: Short-circuiting is not an optimisation detail, it is a tool you use on purpose.
a && a.bis a guard,a || fallbackis a default, and both work because the operators stop early and hand back an operand. Once you see them this way, a great deal of real JavaScript stops looking cryptic.
EXERCISE
3The Nullish Operator, for When Zero Is a Real Value
Using or for defaults breaks whenever the real value is zero or an empty string, and a newer operator fixes exactly that.
|| falls back whenever the left side is falsy. That includes 0 and "", which are often perfectly valid values.
The bug:
const settings = { volume: 0, username: "" };
const volume = settings.volume || 50;
console.log(volume); // 50 <- wrong! The user chose 0.
const name = settings.username || "Anonymous";
console.log(name); // Anonymous <- maybe wrong too
The fix, written with two question marks:
const volume = settings.volume ?? 50;
console.log(volume); // 0 <- correct, 0 is a real choice
const missing = settings.brightness ?? 50;
console.log(missing); // 50 brightness is undefined, so fall back
// ?? falls back ONLY for null and undefined.
Side by side:
console.log(0 || "fallback"); // "fallback"
console.log(0 ?? "fallback"); // 0
console.log("" || "fallback"); // "fallback"
console.log("" ?? "fallback"); // ""
console.log(null || "fallback"); // "fallback"
console.log(null ?? "fallback"); // "fallback"
console.log(undefined ?? "fallback"); // "fallback"
Its partner, optional chaining:
const user = { profile: null };
// console.log(user.profile.city); // TypeError
console.log(user.profile?.city); // undefined, no crash
console.log(user.profile?.city ?? "Unknown"); // Unknown
Pro Tip: Ask what "missing" means for the value in front of you. If zero, an empty string or false are impossible for it,
||is fine. If any of them is a legitimate value, and for a volume, a count, a price or a search box they usually are, use??. This is one of the most common real bugs in modern JavaScript.
