Chapter 5
Controlling Flow
IN THIS CHAPTER
Using if and else
Considering your options with switch
Creating loops
Breaking and continuing loops
“When you have to make a choice and don't make it, that is in itself a choice.”
—WILLIAM JAMES
In previous chapters, you may have read about JavaScript variables, expressions, and statements. These are the essential building blocks of every JavaScript program. However, without the ability to make decisions between various statements, or to repeat statements, programming would be much less interesting. In this chapter, you'll learn about looping and branching statements.
Choosing a Path
Conditional statements, also known as branching statements, allow you to write code that performs various actions based on various conditions.
if … else statements
The if
and else
statements evaluate an expression and then run one block of code if the expression is truthy and another block of code if it's falsy. For example, if you want to customize the display of the current temperature, you could write the following statement:
if (country === "United ...
Get JavaScript All-in-One For Dummies 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.