1 00:00:00,000 --> 00:00:09,848 [MUSIC] 2 00:00:09,848 --> 00:00:13,800 Hi, everyone, I'm Guil, a developer and instructor here at Treehouse. 3 00:00:13,800 --> 00:00:16,740 In this course, I'll teach you another fundamental concept 4 00:00:16,740 --> 00:00:20,560 that you'll use just about every day in JavaScript programming, loops. 5 00:00:20,560 --> 00:00:24,216 [MUSIC] 6 00:00:24,216 --> 00:00:28,938 In programming, a loop is a way to repeat the same set of actions a certain number 7 00:00:28,938 --> 00:00:32,760 of times or until a specific condition is true. 8 00:00:32,760 --> 00:00:37,350 For example, let's say you wanted to display ten random numbers on a web page. 9 00:00:37,350 --> 00:00:40,450 Well, you could write the code to generate a random number, 10 00:00:40,450 --> 00:00:42,270 then display it on the page. 11 00:00:42,270 --> 00:00:45,680 Then write that exact code a second time, a third time, 12 00:00:45,680 --> 00:00:49,470 a fourth time, until you've duplicated that same code ten times. 13 00:00:49,470 --> 00:00:53,780 Well, typing the same code ten times seems like a lot of work, not only for you, but 14 00:00:53,780 --> 00:00:57,040 also for other developers who might work with your code. 15 00:00:57,040 --> 00:00:59,918 Now, imagine you wanted 100 random numbers or 16 00:00:59,918 --> 00:01:03,310 a 1,000, that would be a lot of repetitive code. 17 00:01:03,310 --> 00:01:07,422 Here's a better way, automate it, write that code once, then have your program 18 00:01:07,422 --> 00:01:13,070 repeat it 10 times, or a 100, or a 1,000 times, that's exactly what a loop does. 19 00:01:13,070 --> 00:01:16,960 Think of a loop as a running track with entrance and exit areas. 20 00:01:16,960 --> 00:01:20,100 Imagine you're putting together a 10,000 meter race, 21 00:01:20,100 --> 00:01:23,870 you could build one really long path for the runners to run on. 22 00:01:23,870 --> 00:01:27,590 But that would take up a lot of space, it would take a long time to build and 23 00:01:27,590 --> 00:01:29,690 would be very expensive. 24 00:01:29,690 --> 00:01:34,290 Or you could create a much shorter path that racers can run around over and 25 00:01:34,290 --> 00:01:35,310 over again. 26 00:01:35,310 --> 00:01:37,518 The oval shape of the track is a loop and 27 00:01:37,518 --> 00:01:42,087 a race requires the runners to run around that track a certain number of times. 28 00:01:42,087 --> 00:01:45,381 When a runner has gone around the track ten times, for example, 29 00:01:45,381 --> 00:01:47,522 then that runner has completed the race. 30 00:01:47,522 --> 00:01:49,699 Programming loops also look like this, 31 00:01:49,699 --> 00:01:53,360 the code you want to run more than once is inside the loop. 32 00:01:53,360 --> 00:01:57,580 When you enter the loop, the code inside it runs once, then it runs again and 33 00:01:57,580 --> 00:02:00,100 again until a particular condition is met. 34 00:02:00,100 --> 00:02:03,280 For example, when the loop has run ten times, the loop ends. 35 00:02:04,360 --> 00:02:06,840 Loops are really common in programming and 36 00:02:06,840 --> 00:02:09,380 there are different ways to write loops in JavaScript. 37 00:02:09,380 --> 00:02:10,950 Some use different mechanisms and 38 00:02:10,950 --> 00:02:16,040 syntax making certain loops a better choice over others in specific situations. 39 00:02:16,040 --> 00:02:20,790 You'll learn three types of loops in this course, the while, do-while, and for loop. 40 00:02:21,920 --> 00:02:24,780 Before we keep going, you should know that I'm going to assume that you 41 00:02:24,780 --> 00:02:27,840 already know the basics of JavaScript variables, strings, 42 00:02:27,840 --> 00:02:29,720 numbers, conditionals, and functions. 43 00:02:29,720 --> 00:02:32,790 So to help you succeed, you should have completed the prerequisites for 44 00:02:32,790 --> 00:02:33,600 this course. 45 00:02:33,600 --> 00:02:35,540 You can review them in the teacher's notes with this video.