# JavaScript Ternary Operator Explained with Examples

**URL:** <https://forum.freecodecamp.org/t/javascript-ternary-operator-explained-with-examples/15973>\
**Category:** freeCodeCamp Contributors\
**Tags:** guide\
**Created:** [June 30, 2016, 7:34pm UTC](https://forum.freecodecamp.org/t/javascript-ternary-operator-explained-with-examples/15973 "2016-06-30T19:34:07Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![camperbot](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/camperbot/32/18364_2.png) [@camperbot](https://forum.freecodecamp.org/u/camperbot)\
**Post date:** [June 30, 2016, 7:34pm UTC](https://forum.freecodecamp.org/t/javascript-ternary-operator-explained-with-examples/15973/1 "2016-06-30T19:34:07Z")

</div>

The Ternary operator replaces an `if` / `else` block in a condensed format. It is also referred to as the conditional operator, inline if (iif), or ternary if. The following is the general format of the ternary operator.

```auto
condition ? expr1 : expr2

```

## Description

If `condition` evaluates to true, the operator resolves to the value of `expr1` ; otherwise, it resolves to the value of `expr2` .

For example, to display a different message based on the value of the isMember variable, you can use the following statements:

```auto
let isMember = true;

let message = (isMember) ? 'Welcome Back!' : 'You need to login'; // 'Welcome Back'

```

![:rocket:](https://forum.freecodecamp.com/images/emoji/twitter/rocket.png?v=2 ":rocket:") [Run Code](https://repl.it/M8Ge/1)

## Running functions with ternary operator

It’s also possible to run functions using ternary operator, which sometimes can be useful and more readable.

```auto
    const runFirst = true;
    (runFirst) ? first() : second();

```

## Chaining using the ternary operator

You can also chain a ternary operator indefinitely, in a similar way to using `else if's` before the final `else` of an an `if` / `else` block. Each time the colon is used to state the `else` part of the ternary operator a new condition can be stated until the final termination condition is used.

```auto
    function displayNum (num) {
     return num === 3 ? 'number is 3' : num === 2 ? 'number is 2' : num === 1 ? 'number is 1 ' : 'number is not in range';
    }

```

To ease readability, the code using ternary operators is often formatted across multiple lines as follows:

```auto
    function displayNum(num) {
        return num === 3
            ? 'number is 3'
            : num === 2
                ? 'number is 2'
                : num === 1
                    ? 'number is 1'
                    : 'number is not in range';
    }

```

This way of using the ternary operator needs to be done sparingly and in the right places, which is why (as is the case with using multiple `else if's` ), it can sometimes lead to more readable code by using a `switch` statement instead.

It is advisable to use the ternary operator only in cases which include both `if` and `else` conditions, otherwise using an `if` block is recommended. Chaining ternary operators without care may cause unforeseen bugs.

---

<div class="post-metadata">

**Author:** ![kevcomedia](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/kevcomedia/32/125737_2.png) [@kevcomedia](https://forum.freecodecamp.org/u/kevcomedia)\
**Post date:** [August 7, 2017, 1:46am UTC](https://forum.freecodecamp.org/t/javascript-ternary-operator-explained-with-examples/15973/2 "2017-08-07T01:46:55Z")

</div>

Closed on August 7, 2017, 1:46am UTC


