If/Else vs Ternary: When to Use Each in Your JavaScript Code

You've learned about if/else statements and understand how to use them for decision-making.

Now, you've come across the ternary operator, which you’ve realized is a shorthand way of writing an if/else statement.

This has left you a bit confused. You're unsure when to use if/else and when to use the ternary operator. Are there any rules or best practices, or does it simply come down to personal preference?

You’re thinking about this because you want to develop good coding habits now, rather than having to correct them later.

Let me help you decide how to choose between the two for different situations.

Use if/else when you need to perform actions rather than compute values. Avoid using the ternary operator if you don't need the result of a computation.

let userRole = "admin";

if (userRole === "admin") {
  console.log("Access granted to admin panel.");
} else {
  console.log("Access denied.");
}

The ternary operator is best for expressions—situations where you need to compute a value based on a condition:

let userRole = "admin";
let accessMessage =
  userRole === "admin" ? "Access granted to admin panel." : "Access denied.";
console.log(accessMessage);

Most developers avoid nesting ternary operators because they make code harder to read and debug.

let userRole = "editor";
let accessMessage =
  userRole === "admin"
    ? "Admin access granted."
    : userRole === "editor"
    ? "Editor access granted."
    : "Access denied.";

console.log(accessMessage);

As you can see, it can quickly become unreadable as nesting grows. Instead, use if/else for such cases.

let userRole = "editor";
let accessMessage;

if (userRole === "admin") {
  accessMessage = "Admin access granted.";
} else if (userRole === "editor") {
  accessMessage = "Editor access granted.";
} else {
  accessMessage = "Access denied.";
}

console.log(accessMessage);

Guidelines to Follow

  • Use if/else when performing actions or for complex conditions.

  • Use the ternary operator for short, straightforward value computations.

  • Avoid nested ternary operators to maintain readability.

Stick to these guidelines for now. As you gain experience, you'll develop your own preferences for what you find readable and effective.

Learn Higher-Order Functions

Write code that is easier to understand and maintain.

You've heard about higher-order functions, but every explanation you've come across is either too technical or too vague. You're searching for something clear and practical, with examples that will help you practice and truly master the concept.

Higher-order functions are key to writing better code, essential for your projects, and expected in interviews. But getting started can be tough.

But what if you could? What if you had access to easy-to-understand content that not only explains higher-order functions but also provides hands-on practice? You’d finally feel confident using them in your projects, knowing exactly what they do and how to leverage them effectively.

It’s true, finding the right resources to learn higher-order functions can be challenging… but it doesn’t have to be.

Get my guide, where you’ll:

  • Understand why higher-order functions are worth learning and how they can transform your approach to writing JavaScript.
  • Gain clarity as I break down higher-order functions into simple, everyday language.
  • Master three important higher-order functions: map, filter, and reduce, with plenty of examples to solidify your understanding.
  • Apply your knowledge with real-world examples that demonstrate how these concepts work together, so you can use them easily in your projects or write better code in your interviews.
  • Get practice exercises to help reinforce what you’ve learned and boost your confidence in understanding the concepts.

Get the Guide