Get to know MDN better
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
The conditional (ternary) operator is the only JavaScript operator that takes three operands: a condition followed by a question mark (?), then an expression to execute if the condition is truthy followed by a colon (:), and finally the expression to execute if the condition is falsy. This operator is frequently used as an alternative to an if...else statement.
An expression whose value is used as a condition.
exprIfTrueAn expression which is executed if the condition evaluates to a truthy value (one which equals or can be converted to true).
exprIfFalseAn expression which is executed if the condition is falsy (that is, has a value which can be converted to false).
Besides false, possible falsy expressions are: null, NaN, 0, the empty string (""), and undefined. If condition is any of these, the result of the conditional expression will be the result of executing the expression exprIfFalse.
One common usage is to handle a value that may be null:
The ternary operator is right-associative, which means it can be "chained" in the following way, similar to an if … else if … else if … else chain:
This is equivalent to the following if...else chain.
| ECMAScript® 2027 Language Specification # sec-conditional-operator |
Enable JavaScript to view this browser compatibility table.
This page was last modified on Jul 8, 2025 by MDN contributors.
Your blueprint for a better internet.
Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation.
Portions of this content are ©1998–2026 by individual mozilla.org contributors. Content available under a Creative Commons license.