Conditional Operator

The conditional operatoropen in new window is a ternary operator (an expression that takes three operands). The first operand serves as the condition that will determine which value of the following two operands will be returned.

condition ? expression1 : expression2

If the condition is true the first value of expression1 will be returned, else the value of expression2 will be returned.

console.log(true ? 'Cat' : 'Dog')   // 'Cat'
console.log(false ? 'Cat' : 'Dog')   // 'Dog'

NOTE

The conditional operator is often used as a shortcut for the if statement.