Chapter 21. Control Structures and Exception Handling
IN THIS CHAPTER
Branching script execution down multiple paths
Looping through ordered collections of data
Applying exception handling techniques
You get up in the morning, go about your day's business, and then end the day by walking the dog, changing into comfy footwear, and relaxing. That's not much different from what a program does from the time it starts to the time it ends. But along the way, both you and a program take lots of tiny steps, not all of which advance the processing in a straight line. At times, you have to control what's going on by making a decision or repeating tasks until the whole job is finished. Control structures are the facilities that make these tasks possible in JavaScript.
Control structures in JavaScript follow along the same lines as they do in many programming languages. Basic decision-making and looping constructions satisfy the needs of just about all programming tasks.
Another vital program control mechanism—error (or exception) handling—is formally addressed in the ECMA-262 language standard. The concept of exception handling was added to the JavaScript version introduced in IE5.5 and NN6, but it is well known to programmers in many other environments. Adopting exception-handling techniques in your code can greatly enhance recovery from processing errors beyond your control, such as those caused by errant user input or network glitches.
If and If. . .Else Decisions
Compatibility: WinIE3+, MacIE3+, ...
Get JavaScript® Bible, Seventh Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.