HTML and CSS Reference
In-Depth Information
EXPLANATION
1
The user is prompted for his or her age. The variable called
answer
is assigned the
value the user enters. (See Figure 5.11.)
2
If the value of
answer
is greater than 12 and also less than 20, the statement en-
closed within the curly braces is executed: an alert box appears displaying
Teen-
agers rock!
(See Figure 5.12.) If the user enters any other value, nothing happens.
Figure 5.11
The user enters his or her age.
Figure 5.12
If the user enters his or her age and it is greater than 12 and less than
20, this alert box appears.
The
||
Operator (Logical OR).
In the English statement “If you have some cash
or
I
have a credit card...” the word
or
is used in the condition. With the
or,
only one of the con-
ditions must be met (hopefully that you have the cash!). JavaScript uses the
||
symbol to rep-
resent the logical OR. If the expression on the left side of the
||
operator is evaluated as true
(nonzero), the value of the expression is true, and no further checking is done. If the value
on the left side of the
||
operator is false, the value of the expression on the right side of the
operator is evaluated, and if true, the expression is true; that is, only one expression must
be true. Once an expression returns true, the remaining expressions can be either true or
false. It doesn't matter, as long as
one expression is true
. Refer to Table 5.13.
Table 5.13
Logical OR Examples
Expression
What It Evaluates To
true || false
true
true || true
true
Continues