Back to JavaScript Notes
Topic #283

Conditional (Ternary) Operator

You will be able to use the JavaScript conditional (ternary) operator to choose between two values in a compact, readable expression.

What it is

The conditional operator, often called the ternary operator, is a compact form of if/else that returns one of two values. It is an expression, so it can be used inside assignments, function arguments, template literals, and return statements. The three parts are a condition, a value for true, and a value for false. Related terms include conditional expression, ternary operator, and inline if.

Why it matters

  • It reduces boilerplate when selecting a value based on a simple condition.
  • It keeps assignments and returns concise, such as setting a label, class name, or message.
  • It can be used directly inside template literals or function calls without a separate statement.
  • It evaluates only the chosen branch, which can avoid unnecessary work or errors.

Syntax or steps

The smallest useful pattern is:

condition ? valueIfTrue : valueIfFalse;
  1. Write a condition that JavaScript can convert to a boolean.
  2. Place ? after the condition.
  3. Write the value to return when the condition is truthy.
  4. Place : after that value.
  5. Write the value to return when the condition is falsy.

Example

const age = 20;
const label = age >= 18 ? "adult" : "minor";
console.log(label); // "adult"

const score = 72;
const grade = score >= 90 ? "A" : score >= 80 ? "B" : "C";
console.log(grade); // "C"

Part by part:

  • age >= 18 is the condition.
  • "adult" is returned when the condition is truthy.
  • "minor" is returned when the condition is falsy.
  • The nested example checks score >= 90, then score >= 80, then falls back to "C".

Common mistakes

  • Using it for side effects instead of returning a value. If you need to run multiple statements, use if/else.
  • Creating unreadable nested ternaries. If the logic needs more than two simple choices, prefer if/else, switch, or a helper function.
  • Forgetting operator precedence. Wrap the whole ternary in parentheses when combining it with other operators, such as const text = (isAdmin ? "admin" : "user") + " mode";.
  • Assuming both branches run. Only the selected branch is evaluated, so isValid ? parse(value) : null avoids calling parse when invalid.

When to use it

Use the ternary operator when you need to choose one value from two options. Use if/else when you need to run statements, handle multiple branches, or improve readability.

PatternBest forExample
Ternary operatorReturning one of two valuesconst status = ok ? "ready" : "blocked";
if/elseRunning statements or complex logicif (ok) { show(); } else { hide(); }
switchMany discrete casesswitch (role) { case "admin": ... }

Practice

Guided exercise: create a variable named temperature with the value 35. Use a ternary operator to set message to "hot" when the temperature is greater than or equal to 30, otherwise "cool".

const temperature = 35;
const message = temperature >= 30 ? "hot" : "cool";
console.log(message); // "hot"

Challenge: write a ternary that returns "even" or "odd" for a number. Hint: use number % 2 === 0 as the condition.

Quick check

Question: What does const result = 0 ? "yes" : "no"; store in result?

Answer: "no", because 0 is falsy.

Summary

The ternary operator is a JavaScript expression that returns one of two values based on a condition. It is useful for compact value selection, but it should be reserved for simple cases where readability remains clear.

Want to go beyond the notes?

Join Coding Now Tech Institute's JavaScript course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

Conditional (Ternary) Operator – FAQs

Quick answers about learning Conditional (Ternary) Operator in JavaScript.

This free note from Coding Now Tech Institute explains Conditional (Ternary) Operator in JavaScript — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every JavaScript topic on Coding Now Tech Institute, including Conditional (Ternary) Operator, is 100% free with no signup required.
With focused practice, most students grasp Conditional (Ternary) Operator in 1–3 days from these notes; pairing it with Coding Now Tech Institute's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the Coding Now Tech Institute Community (/community) — expert instructors answer within 24 hours.
Call NowEnroll Now