The JavaScript Conditional Operator

This operator can be used to write compact code forks in JavaScript. It takes the form

(condition)?trueStatement:falseStatement

where

The conditional operator is often considered dangerous. In our view - there are no dangerous operators, only dangerous programmers. Using this operator can result in compact code that is easy to read and to maintain. Browsers can often fail to figure out the logic of a carelessly written JavaScript conditional statement. However, this is easily avoided through a judicious use of parentheses. Study the examples below for safe and correct use of this operator.

Colophon