Home » Blog » Conditional (Ternary) Operator in JavaScript Explained with Examples

Conditional (Ternary) Operator in JavaScript Explained with Examples

Learn how to create a JavaScript shorthand if using the conditional (ternary) operator.

When you are creating conditions in your JavaScript code, most of the time, you’re using traditional if, else, and else if statements.

Thanks to the conditional operator in JavaScript (also called ternary operator), you will write shorthand if else statements.

If you like short video content, this article is also available on Youtube! 🎥

Conditional (ternary) operator syntax

A ternary condition in JavaScript is represented by ? and : in your code.

If you want to understand how it works, you can compare it to a short if else JavaScript statement:

// JavaScript traditional statement:
if (something) {
  console.log('Yes')
} else {
  console.log('No')
}

// JavaScript ternary statement:
something ? console.log('Yes') : console.log('No')
// [condition] ? [if] : [else]

To summarize, the conditional operator in JavaScript is a if else shorthand.

Below, you will find examples to understand how and when to use this conditional operator.

JavaScript shorthand if else

The code below is the most common use case. It happens when you have a short condition, and you want to keep your code concise. In that case, you can use this inline if statement:

const name = 'Julien'

// JavaScript if else shorthand:
const color = name === 'Julien' ? 'blue' : 'green'
// if the variable `name` is equals to "Julien" the colour is "blue" otherwise it's "green"

console.log(color)
// Output: "blue"

Ternary operator with multiple conditions

Conditional operator in JavaScript also allows you to create multiple conditions.

Below, you will find an example using the ternary operator.

const age = 20

// This multiple conditions with the ternary operator:
age <= 25
  ? age <= 10
    ? console.log('less or equal to 10')
    : console.log('less or equal to 25 but more than 10')
  : console.log('too old')
// Output: "less or equal to 25 but more than 10"

// Is the same than this `if` `else` multiple conditions:
if (age < 25) {
  if (age < 10) console.log('less or equal to 10')
  else console.log('less or equal to 25 but more than 10')
} else {
  console.log('too old')
}
// Output: "less or equal to 25 but more than 10"

As you can notice, it’s hard to read. Suppose your condition is more than one condition, then you should avoid using the ternary operator. Otherwise, your code can become unreadable and hard to maintain.

Ternary operator with multiple operations

The JavaScript ternary operator also works to do multiple operations in one statement.

It’s the same as writing multiple operations in an if else statement.

let letter = 'a'

// This ternary operator with multiple operations:
letter === 'a'
  ? (console.log('letter is a'), console.log('this is cool'))
  : console.log('letter is not a')
// Output:
// "letter is a"
// "this is cool"

// Is the same than this `if` `else` multiple operation conditions:
if (letter === 'a') {
  console.log('letter is a')
  console.log('this is cool')
} else {
  console.log('letter is not a')
}
// Output:
// "letter is a"
// "this is cool"

Like the ternary operator with multiple conditions, I don’t recommend this usage.

Conditional (ternary) operator summary

In summary, the conditional operator in JavaScript is powerful, but you should use it with precaution.

One of the main advantages of using it is to write shorthand if else statements. If you have a short condition and want to do it in one line, the ternary operator will be perfect.


Thanks for reading. Let’s connect!

➡️ I help you grow into Web Development, and I share my journey as a Nomad Software Engineer. Join me on Twitter for more. 🚀🎒

Gaël Thomas

🚀 Help you grow into Web Development 👨‍💻 Share about my journey as Nomad Software Engineer 🧰 Into JavaScript, TypeScript, Node, and React

Post navigation