Subscribe via RSS!

What is the "Ternary" operator, and how is it used?

As opposed to a Binary operator which focus on two elements (such as "foo > bar" or "dollars / hour) a Ternary operator instead focuses on 3 elements. The ternary operator has 2 symbols, the question mark "?" and the colon ":" the syntax looks like this

1
Condition ? true : false

This is the equivilent of a mini if/else statement. For example, the following code with if/else statements could be refactored with the ternary operator.

1
2
3
4
5
6
7
8
9
var playerOne = 500;
var playerTwo = 600;

 var highScore;
 if (playerOne > playerTwo){
 highScore = playerOne;
 } else {
 highScore = playerTwo
 }

The above code is equivilant to the following code, which replaces if/else statements with a single ternary operator

1
2
3
4
5
var playerOne = 500;
var playerTwo = 600;

var highScore = (playerOne > playerTwo) ? playerOne : playerTwo ;
 

The condition is "Is the value of playerOne greater than playerTwo?" The result can either be true or false. If it's true, we will assign the value of playerOne to the variable highScore. If it's false, we will assign the value of playerTwo to the variable highScore.


Follow me!

©2017 Miles Rose