Chapter 8

Making Choices in JavaScript

In This Chapter

arrow Considering when to make a decision

arrow Using the if structure to make decisions

arrow Using the switch structure to make decisions

Decisions, decisions — life is all about decisions. Because applications mimic real life in many respects, your application needs to make lots of decisions too. However, unlike the impromptu decisions you make, applications require preprogrammed decisions. You decide in advance which decisions the application makes and for what reasons. In addition, you decide under what conditions the application makes the decisions and what happens when a certain decision path is taken. The only variable is the data used to make the decision — you can’t easily control the data, which is why you need a decision-making technique in the first place.

Application decisions are expressed as statements. A statement consists of a keyword, which is followed by an expression that defines the decision to make. Following the statement is a code block that contains one or more tasks to perform when an expression is true. The combination of the statement and the code block is called a structure. Consequently, when you discuss the

Get HTML5 Programming with JavaScript For Dummies now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.