HTML and CSS Reference
In-Depth Information
Figure 6.2 If the age entered was greater than 55, this message is displayed.
The Conditional Operator. The conditional operator, called a ternary operator, was
discussed in Chapter 5, “Operators.” Because it is often used as a shortcut for the if/else
conditional statement, it is reviewed again here.
FORMAT
conditional expression ? expression : expression
EXAMPLE
x ? y : z
If x evaluates to true, the value of the expression
becomes y, else the value of the expression becomes z
big = (x > y) ? x : y
If x is greater than y , x is assigned to
variable big , else y is assigned to
variable big
An if/else statement instead of the conditional statement:
if (x > y) {
big = x;
}
else{
big = y;
}
EXAMPLE 6.2
<html>
<head>
<title>Conditional Operator</title>
</head>
<body bgcolor="lightblue">
<big>
<script type ="text/javascript">
1
var age = prompt("How old are you? ", "");
2
var price = (age > 55 ) ? 0 : 7.50;
 
Search WWH ::




Custom Search