Chapter 4
Controlling the Flow of JavaScript
IN THIS CHAPTER
Understanding how you control the flow of JavaScript
Setting up your code to make decisions
Understanding code looping
Setting up code loops
Avoiding the dreaded infinite loop
A good programmer is someone who always looks both ways before crossing a one-way street.
— DOUG LINDER
When the web browser comes across a <script>
tag, it puts on its JavaScript hat and starts processing the statements. Not surprisingly, the browser doesn't just leap randomly around the script, parsing the statements willy-nilly. That would be silly. No, the browser puts its head down and starts processing the statements one at a time: the first statement, the second statement, and so on until there’s no more JavaScript left to parse.
That linear statement-by-statement progression through the code makes sense, but it doesn’t fit every situation. Sometimes you want your code to test some condition and then run different chunks of code depending on ...
Get Web Coding & Development All-in-One For Dummies, 2nd 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.