Skip to Content
JavaScript® 24-Hour Trainer
book

JavaScript® 24-Hour Trainer

by Jeremy McPeak
December 2010
Beginner
455 pages
9h 37m
English
Wrox
Content preview from JavaScript® 24-Hour Trainer

Chapter 5. Loops

In Lesson 3, you learned how functions can greatly clean up your code by encapsulating repetitive operations and calculations to use throughout the application's code. There are times, however, when a function isn't the right tool for handling repetitive code.

For example, you want a particular line of code repeated 10 times — no more and no less. You can, of course, write that same line of code 10 times, but in doing so you condemn yourself to a maintenance nightmare in which you may have to make 10 changes to 10 lines of code. Functions don't solve the problem either. Although the code might be a little less complex, you still have to type (or copy and paste) 10 identical lines of code.

For this reason, programming languages have a concept called looping. In programming, looping means to repeat one or more lines of code while a particular condition is true. This functionality is useful, and you'll see loops used throughout the book. But for now, let's take a look at some loops.

LOOP BASICS

There are several different types of loops, but they all perform the same primary function of repeating code. They also share some similar features. For example, all loops repeat a code block based on the results of a condition. As long as the condition evaluates to true, the loop iterates, or repeats its code, and it immediately ends when the condition evaluates to false.

Loops can be grouped into two categories: pre-test and post-test. A pre-test loop tests its condition at the ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

JavaScript® 24-Hour Trainer

JavaScript® 24-Hour Trainer

Jeremy McPeak
The Human Factor in AI-Based Decision-Making

The Human Factor in AI-Based Decision-Making

Philip Meissner, Christoph Keding

Publisher Resources

ISBN: 9780470647837Purchase book