Chapter 3. Adding Logic and Control to Your Programs
So far youâve learned about some of JavaScriptâs basic building blocks. But simply creating a variable and storing a string or number in it doesnât accomplish much. And building an array with a long list of items wonât be very useful unless thereâs an easy way to work your way through the items in the array. In this chapter, youâll learn how to make your programs react intelligently and work more efficiently by using conditional statements, loops, and functions.
Making Programs React Intelligently
Our lives are filled with choices: âWhat should I wear today?â, âWhat should I eat for lunch?â, âWhat should I do Friday night?â, and so on. Many choices you make depend on other circumstances. For example, say you decide you want to go to the movies on Friday night. Youâll probably ask yourself a bunch of questions like âAre there any good movies out?â, âIs there a movie playing at the right time?â, âDo I have enough money to go to the movies (and buy a $17 bag of popcorn)?â
Suppose there is a movie thatâs playing at just the time you want to go. You then ask yourself a simple question: âDo I have enough money?â If the answer is yes, youâll head out to the movie. If the answer is no, you wonât go. But on another Friday, you do have enough money, so you go to the movies. This scenario is just a simple example of how the circumstances around us affect the decisions we make.
JavaScript has ...
Get JavaScript: The Missing Manual 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.