Ternary Operator

ICE Data Services -

Ternary Operator

 

The one-line IF statement in Javascript (also known as a ternary operator) can be a real time saver.

 

Example:

 

var MyVal;

 

MyVal = x > y ? 10 : -6 ;

 

The one-line IF statement consists of 3 parts:

 

Part1 - the conditional statement. In the above example, this would be x > y

 

Part2 - the return value IF the conditional statement is true. In the above example, this would be the number 10

 

Part3 - the return value IF the conditional statement is false. In the above example, this would be the number -6

 

So, in the above example, if the variable x is greater than the variable y, our MyVal variable will be loaded with the value of 10, otherwise our MyVal variable will be loaded with the value of -6