Name

?: — NN 2 IE J1 ECMA 1

Synopsis

The conditional operator. This operator provides a shortcut syntax to an if/else control structure. There are three components to the deployment of this operator: a condition and two statements. If the condition evaluates to true, the first of the statements is executed; if the condition evaluates to false, the second statement is evaluated. The syntax is as follows:

                     condition ? statement1 : statement2

This operator is a shortcut in appearance only. It invokes the same internal processing as an if...else construction.

Example

var newColor = (temp > 100) ? "red" : "blue"

Get Dynamic HTML: The Definitive Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.