DEV Community

Cover image for Short-Circuit Evaluation in JavaScript: && and || Made Simple
Shifana Abdul Khader
Shifana Abdul Khader

Posted on

Short-Circuit Evaluation in JavaScript: && and || Made Simple

🧠 Introduction:

Have you ever seen code like this?

user && user.name
Enter fullscreen mode Exit fullscreen mode

or

const name = input || 'Guest';
Enter fullscreen mode Exit fullscreen mode

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

Enter fullscreen mode Exit fullscreen mode

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!
Enter fullscreen mode Exit fullscreen mode

❓What actually happens?

  • useris null, which is falsy.

  • So JavaScript sees null && user.name and says:

β€œHmm,user is falsy. I'm not even going to checkuser.name."

  • It returns user(which is null) β€” 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';
Enter fullscreen mode Exit fullscreen mode

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

Enter fullscreen mode Exit fullscreen mode
  • '' 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);
}
Enter fullscreen mode Exit fullscreen mode

βœ… 2. Default values:

const theme = settings.theme || 'light';
Enter fullscreen mode Exit fullscreen mode

βœ… 3. Guarding a function call (Conditional Execution):

isLoggedIn && showDashboard();
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

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)