π§ Introduction:
Have you ever seen code like this?
user && user.name
or
const name = input || 'Guest';
And wondered what it really does?
This is called short-circuit evaluation in JavaScript, and itβs a powerful way to write cleaner, safer, and smarter code. Let me break it down in the simplest way possible!
π Prerequisite: Truthy & Falsy Values in JavaScript
In JavaScript, every value is either βtruthyβ or βfalsyβ when used in conditions.
β Falsy values (evaluated as false):
false,0, '', null, undefined, NaN
β Truthy values (evaluated as true):
'hello', 1, -5, {} (empty object), [] (empty array)
Any non-zero number or non-empty string
πΈ What is Short-Circuit Evaluation?
JavaScript uses short-circuit logic to decide whether to evaluate the second part of a logical expression. Simple :)
There are two main operators:
&&β AND operator||β OR operator
1. && (AND): Stops if the first value is falsy
user && user.name
What it means:
If userexists (is truthy), then give me user.name.
If useris falsy (like nullor undefined), stop right there and return that falsy value.
π Example:
let user = null;
console.log(user && user.name); // Output: null - it doesn't crash!
βWhat actually happens?
userisnull, which is falsy.So JavaScript sees
null && user.nameand says:
βHmm,user is falsy. I'm not even going to checkuser.name."
- It returns
user(which isnull) β thatβs what&&does.
If we just wrote user.name, it would give an error: β TypeError: Cannot read property 'name' of null
β
But user && user.nameis safe!
2. || (OR): Stops if the first value is truthy
const name = input || 'Guest';
What it means:
If inputhas a value (truthy), use that. Otherwise, use 'Guest' as default.
π Example:
let input = '';
let name = input || 'Guest';
console.log(name); // Output: Guest
-
''is falsy, so it moves on to'Guest'.
π‘ Real-Life Examples
β
1. Safe Access β Optional chaining-style check (before optional chaining was introduced):
if (user && user.address && user.address.city) {
console.log(user.address.city);
}
β
2. Default values:
const theme = settings.theme || 'light';
β
3. Guarding a function call (Conditional Execution):
isLoggedIn && showDashboard();
Only runs showDashboard() if isLoggedIn is true.
β οΈ Common Mistakes to Avoid
Donβt use || if your first value might be 0, false, or '' and is actually valid!
Letβs say:
const age = userAge || 18; // This sets 18 even if userAge is 0
But what if userAgeis 0(valid age)?
Since 0is falsy, this will replace it with 18π¬
β
Instead, Fix using the Nullish Coalescing Operator:
const age = userAge ?? 18; // Nullish coalescing operator (better in modern JS)
This only replaces nullor undefinedβ not 0or false.
π¬ Conclusion
Short-circuiting lets you write safe, clean, and efficient JavaScript. Just remember:
&&β Stops at *false *(good for checking before accessing).||β Stops at *true *(good for defaults).
The key thing ,
These operators return actual values, not just trueor false.
Try it out in your own projects and see how much cleaner your code gets!
Happy coding! π»
Top comments (0)