Operators

Conditional operator (? :)

Try the free JavaScript compiler

Tests the truthiness of the first expression and evaluates only the selected result expression. The result is the value of that branch. Nested conditionals group to the right.

Formal Syntax & Structure

javascript syntaxjavascript
test ? consequent : alternate

Parameters & Attributes

NameTypeDescription
testexpressionValue tested for truthiness.
consequentexpressionEvaluated for a truthy test.
alternateexpressionEvaluated for a falsy test.

Return Value

selected value

Value of the selected expression.

Working Example

Example Snippetjavascript
console.log(3 > 2 ? "yes" : "no");
JavaScript Reference