Loops and Conditionals

Loops and conditionals give programs the power to make decisions and perform tasks multiple times. JavaScript provides the standard conditionals and looping structures that are available in many computer languages. In fact, these structures were patterned after those found in C, C++, and Java, so if you have written code in any of these languages, you will find this section very straightforward.

Conditionals

Conditional statements enable programs to make decisions based on preset conditions that use the operators discussed earlier in the chapter.

if

The if statement is by far the most common conditional statement simply because it is simple and easy to use. The format of a simple if statement looks like the following: ...

Get Pure JavaScript 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.