Introduction to JavaScript

Incrementing in Loops

Introduction to JavaScript

Check out a free preview of the full Introduction to JavaScript course

The "Incrementing in Loops" Lesson is part of the full, Introduction to JavaScript course featured in this preview video. Here's what you'd learn in this lesson:

Brian uses what was learned in the previous lesson to demonstrate how a 'for' loop is executed.

Preview
Close

Transcript from the "Incrementing in Loops" Lesson

[00:00:00]
>> Brian Holt: Using the same kind of example here, we say let friendsAtYourParty equals zero. And then we're gonna do what's called a for loop. So I'm gonna put for, F-O-R. I'm gonna say, let i equals zero,
>> Brian Holt: Semicolon.
>> Brian Holt: I is less than or equal to 10, semicolon, I plus plus.

[00:00:31]
And you're gonna say friendsAtYourParty plus plus.
>> Brian Holt: So let's dissect this madness right here.
>> Brian Holt: If for loop has three different parts inside of the parenthesis here, always has three parts. The first part, you're declaring your control variables, right? So this i variable is going to be the control variable for this particular loop.

[00:01:01]
It's always i, it doesn't have to be i but it always is. [LAUGH] So I can come in here and I could totally make this like, no counter or something like that. And as long as I'm consistent,
>> Brian Holt: This works, right? So you can call it whatever you want.

[00:01:21]
It's just always called i. [LAUGH] Across programming languages, my entire life, it's always been i. So I would just implore you to stick with i, because all of your coworkers will probably do it that way as well.
>> Brian Holt: Okay, so the first one you declare your control variables.

[00:01:38]
So in this case we are saying let i and start i at 0,
>> Brian Holt: The second one is the control statement which is going to be as long as i is less than or equal to 10, right? So keep doing this until i is less than or equal to 10.

[00:01:54]
And then the last thing is run this statement at the end of every loop, right? So at the end of every loop, do i plus plus, right? So very similar to what we were doing with the while loop before, right? We were just checking to see if friendsAtYourParty got greater than ten, and once it was greater than ten then it stopped.

[00:02:17]
>> Brian Holt: So same thing happening here.
>> Brian Holt: And actually, so that they're all considered equal we'll do,
>> Brian Holt: Less than ten. So same thing here for your fresh and evaluate, it's gonna be ten.
>> Brian Holt: So kind of just walk you through the iteration here. The first thing it's gonna ask is, is 0 less than or equal to 10?

[00:02:54]
It's not, so it goes into the loop, it runs this particular thing friendsAtYourParty plus plus. So this gets implemented from zero to one, right? It finishes the loop. It runs i plus plus. So now i is one, right? Then it's gonna go through and ask this question again, is one greater than ten?

[00:03:12]
It's not, so it is going to go through in here, run friends plus plus again and it is going to keep running that loop over and over and over again, until this is ten, right? Once it is ten, then you've completed the loop and it is going to finish the loop here.

[00:03:32]
>> Brian Holt: Yeah?
>> Speaker 2: I don't think I've ever seen let i in a for loop. Why will it let you not use let? [LAUGH]
>> Brian Holt: [LAUGH]
>> Speaker 2: Cuz I could just say for i equals zero, so I get why you're using let for the other variables, cuz you can change them.

[00:03:48]
>> Brian Holt: Like this?
>> Speaker 2: Yeah.
>> Brian Holt: So the reason why you don't wanna do it this way, this does work, and we'll get into this momentarily here, but it's still is basically implicitly creating this i for you. But now it's no longer scoped to this particular for loop, which is now what you want.

[00:04:11]
And I will qualify that here in just a second because we're about to talk about scope. Suffice to say you should say, let i equal zero here. So inside of here you can actually say console.log i, and you can actually use the i inside of here. So what would you expect here to happen?

[00:04:35]
It's gonna count from zero to ten.
>> Brian Holt: We'll console.log this last one out here. So it goes from zero to nine here, right? So it actually does ten iterations if zero counts as well. So for loops are very useful for if you have to do something a defined amount of time.

[00:04:54]
If I have to do something ten times, this is a very easy way to do something exactly ten times.
>> Brian Holt: And hence, I would say for loops are probably the ones you're gonna use the most. I don't very frequently do while loops, very frequently I do for loops.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now