some parameter appropriately during the course of the day. We might write a
that can accept some sort of input, and act accordingly. Or we might write a
that repeats a specified action.
We can freely test values that are of Boolean, numeric, or string type here in
In if statements a condition is tested, and if the condition
In the if...else statement, different code is executed if the
condition fails the test. The simplest form of an if statement
can be written entirely on one line, but multilane if and if...
else statements are much more common in web development.
and if...else statements. The first example shows the simplest
kind of Boolean test. If (and only if) the item between the
parentheses evaluates to true, the statement or block of statements
after the if is executed.
// The ifTest() function is defined
elsewhere in the code.
forMaleOnly(" Male Test of If is true."); // Boolean test of whether
(male) is true.
// In this example, the test fails unless both conditions are true.
if (selected.color == "deep green " && selected.texture == "medium
theAnswer = ("Is it a nice
flower? <br> ");
// In this example, the test succeeds if either condition is true.
var theAnswer = "";
if ((lbsWeight > 15) || (lbsWeight > 45))
theAnswer = ("Oh, what a cute kitty! <br>");
theAnswer = ("That's one huge cat you've got there! <br>");
the condition to be tested rather than the word if before the condition, and specifies
two alternatives, one to be used if the condition is met and one if it is not. The
var hours = "";
// Code specifying that hours contains either the contents of
// theHour, or theHour - 12.
hours += (theHour >= 12) ? " PM" : " AM";
we know that
one is more likely to pass or fail than any of the others, depending on whether
testing stops as soon as any one condition passes the test. This is particularly
effective if the conditions to be tested involve execution of
An example of the side effect of short-circuiting is that runsecond will not be
executed in the following example if runfirst() returns 0 or false as shown
if ((runfirst() == 0) || (runsecond() == 0))