Javascript Reference
Categories: Operators

javascript Operators ?:

@March 15, 2008, 1:22 a.m.
?: Firefox/Netscape/NN 2 IE 3 ECMA 1  

  

The conditional 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
 

You can nest these operators as a way of adding more decision paths within a single statement. In the following syntax, if conditionA evaluates to false, conditionB is evaluated, and the entire expression returns the value of statement2 or statement3 depending on the results of conditionB.

 

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";

Powered by Linode.