DEV Community

Jeje ✨ (Джиджи)
Jeje ✨ (Джиджи)

Posted on

What is Ternary Operator in JavaScript?

If you're learning JavaScript and came across something like this:

let message = age > 18 ? "Welcome!" : "Sorry, you're too young."; 
Enter fullscreen mode Exit fullscreen mode

...and thought, "What is that question mark doing in the middle of the code?"
You're not alone!

That is called a ternary operator.It might look strange at first, but it's actually a simple and powerful way to write decisions in code.

Let's break it down step by step.

What is the Ternary Operator?

The ternary operator is a conditional operator that evaluates a condition and returns one of two values depending on whether the condition is true or false.

It has this structure

condition ? expressionIfTrue : expressionIfFalse; 
Enter fullscreen mode Exit fullscreen mode

In other words, the ternary operator is a shortcut for writing a simple if-else statements in one line.
It's called ternary because it uses three parts:

  1. A condition (something that is either true or false)
  2. A result if the condition is true
  3. A result if the condition is false

Example

Let's say you're writing a program to check someone's age:

let age = 20; let message = age >= 18 ? "You can vote." : "You are too young to vote."; console.log(message); // Output: "You can vote." 
Enter fullscreen mode Exit fullscreen mode

Here's what this code does:

  • It checks: is age 18 or more?
  • If yes, it gives the message: "You can vote."
  • If no, it gives: "You're too young to vote."

It's the same as writing:

let message; if (age >= 18) { message = "You can vote."; } else { message = "You're too young to vote."; } 
Enter fullscreen mode Exit fullscreen mode

But shorter and cleaner!

When is it a good idea?

Ternary Operator is not meant to replace if-else statement. Use it when:

  • You want to choose between two values
  • You are assigning a value (like a message or label)
  • The decision is simple (easy to understand)
  • You want to write it all in one line

Example:

let isMember = true; let price = isMember ? "$20" : "$35"; 
Enter fullscreen mode Exit fullscreen mode

When should you avoid it?

Sometimes using the ternary operator can make your code harder to read, especially when:

  • The condition is too long or complex
  • You're stacking multiple ternaries on top of each other
  • You're not just returning values, but doing actions (like showing messages, logging in, etc.)

Example of what NOT to do:

let result = score > 90 ? "A" : score > 80 ? "B" : score > 70 ? "C" : "F"; 
Enter fullscreen mode Exit fullscreen mode

In this case, it's better to use regular if-else blocks for things like this.

Final Verdict

The ternary operator is like a mini decision-making machine in JavaScript. Once you get the hang of it, you'll find it super useful for simple yes or no style logic.

Just remember these best practices:

  • use ternary for simple value decisions
  • keep it short and readable
  • Don't let it become a puzzle others have to solve
  • Don't use it to replace full blocks of logic ** Avoid nested ternaries unless absolutely necessary

If you can read it easily a day later, you're probably using it right. 😊

Top comments (0)