The concept of Program Control

So far, we have seen ways to change the appearance and behavior of Objects on a Web page in direct response to user input. But we haven't seen how JavaScript can be used to make decisions, or process and simplify complex data. These are two of the most important things you can do with JavaScript. In this module we'll look at why this is true.

The "if" statement

"if" statements are used to compare two values and execute Functions based on the result. This gives you the ability to make decisions based on all kinds of different input.

For instance: Is the user a first-time visitor? Or not? Has a shopper ever ordered a certain item? Or not? Has a customer supplied their phone number? Or not?

In each of these cases, you would want to execute different actions in response to the answers. You can use "if" statements to do this.

Looping.

Looping is when you repeat an action over and over again. And it's amazing what you can accomplish by doing simple things repetitively. Basically, if you have a lot of information to process, you can use loops to systematically work through it.

For instance, you can use loops to retrieve information from users by working through all of the form elements on a page and putting the data from each into a Variable. Or you can present complex information to users by using a loop to control a document.write() Method that generates a very detailed Web page (such as a price list with hundreds of items).

The combination of the two.

When you combine "if" statements with looping, you gain the ability to greatly simply the user's interaction with your Web site. You can collect user information, process it, make decisions about it and then generate tailored responses.

With these new techniques, you also gain a greater ability to control the various techniques that you have already learned.