Ternary Operator (JavaScript)

The ternary operator is a one-line shortcut for an if/else that returns a value. It uses the form condition ? valueIfTrue : valueIfFalse.

JavaScript

When you need to choose between two values based on a condition, the ternary operator is more compact than a full if/else block.

Keep ternaries simple — nesting them quickly becomes hard to read.

Syntax

condition ? valueIfTrue : valueIfFalse

Examples

JavaScript

const age = 20;
const status = age >= 18 ? "adult" : "minor";
console.log(status);

Output:

adult

See also

Back to the Dictionary