Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
      You have completed JavaScript Loops!
      
    
You have completed JavaScript Loops!
Preview
    
      
  In programming, a loop is a way to repeat the same set of actions a certain number of times or until a specific condition is true.
This video doesn't have any notes.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
                      [MUSIC]
                      0:00
                    
                    
                      Hi, everyone, I'm Guil, a developer and
instructor here at Treehouse.
                      0:09
                    
                    
                      In this course,
I'll teach you another fundamental concept
                      0:13
                    
                    
                      that you'll use just about every day
in JavaScript programming, loops.
                      0:16
                    
                    
                      [MUSIC]
                      0:20
                    
                    
                      In programming, a loop is a way to repeat
the same set of actions a certain number
                      0:24
                    
                    
                      of times or
until a specific condition is true.
                      0:28
                    
                    
                      For example, let's say you wanted to
display ten random numbers on a web page.
                      0:32
                    
                    
                      Well, you could write the code
to generate a random number,
                      0:37
                    
                    
                      then display it on the page.
                      0:40
                    
                    
                      Then write that exact code a second time,
a third time,
                      0:42
                    
                    
                      a fourth time, until you've
duplicated that same code ten times.
                      0:45
                    
                    
                      Well, typing the same code ten times seems
like a lot of work, not only for you, but
                      0:49
                    
                    
                      also for other developers who
might work with your code.
                      0:53
                    
                    
                      Now, imagine you wanted
100 random numbers or
                      0:57
                    
                    
                      a 1,000,
that would be a lot of repetitive code.
                      0:59
                    
                    
                      Here's a better way, automate it, write
that code once, then have your program
                      1:03
                    
                    
                      repeat it 10 times, or a 100, or a 1,000
times, that's exactly what a loop does.
                      1:07
                    
                    
                      Think of a loop as a running track
with entrance and exit areas.
                      1:13
                    
                    
                      Imagine you're putting
together a 10,000 meter race,
                      1:16
                    
                    
                      you could build one really long path for
the runners to run on.
                      1:20
                    
                    
                      But that would take up a lot of space,
it would take a long time to build and
                      1:23
                    
                    
                      would be very expensive.
                      1:27
                    
                    
                      Or you could create a much shorter path
that racers can run around over and
                      1:29
                    
                    
                      over again.
                      1:34
                    
                    
                      The oval shape of the track is a loop and
                      1:35
                    
                    
                      a race requires the runners to run around
that track a certain number of times.
                      1:37
                    
                    
                      When a runner has gone around
the track ten times, for example,
                      1:42
                    
                    
                      then that runner has completed the race.
                      1:45
                    
                    
                      Programming loops also look like this,
                      1:47
                    
                    
                      the code you want to run more
than once is inside the loop.
                      1:49
                    
                    
                      When you enter the loop, the code inside
it runs once, then it runs again and
                      1:53
                    
                    
                      again until a particular condition is met.
                      1:57
                    
                    
                      For example, when the loop has
run ten times, the loop ends.
                      2:00
                    
                    
                      Loops are really common in programming and
                      2:04
                    
                    
                      there are different ways to
write loops in JavaScript.
                      2:06
                    
                    
                      Some use different mechanisms and
                      2:09
                    
                    
                      syntax making certain loops a better
choice over others in specific situations.
                      2:10
                    
                    
                      You'll learn three types of loops in this
course, the while, do-while, and for loop.
                      2:16
                    
                    
                      Before we keep going, you should know
that I'm going to assume that you
                      2:21
                    
                    
                      already know the basics of
JavaScript variables, strings,
                      2:24
                    
                    
                      numbers, conditionals, and functions.
                      2:27
                    
                    
                      So to help you succeed, you should
have completed the prerequisites for
                      2:29
                    
                    
                      this course.
                      2:32
                    
                    
                      You can review them in
the teacher's notes with this video.
                      2:33
                    
              
        You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up