Archive for June 16, 2007

JavaScript Conditional Operator (?:) Question Mark, Colon

Posted in JavaScript, Software, Web, internet, programming on June 16, 2007 by Joey

JavaScript provides the conditional operator as a shortcut for an “if” statement. The conditional operator consists of a question mark (?) and a colon (:), as shown below:

var intTest = 1;
intTest == 1 ? x = 1 : x = 2;
document.write(x);

The expression preceding the question mark must evaluate to a Boolean value. If the result of this expression is true, the expression following the question mark but preceding the colon runs; otherwise, the expression after the colon runs.