Learn JavaScript #8 | For Loops

JosephDelgadillo
58 followers
4 Video Views·Aug 20, 2024
Get The Learn to Code Course Bundle!
https://josephdelgadillo.com/product/learn-to-code-course-bundle/
In the 8th episode of this JavaScript tutorial series, we will cover for loops. A for loop is a way to do something repetitively until a certain threshold is met. In this video, I will show you the basic syntax in JavaScript, and we will write a conditional for loop.
Learn more about my courses at https://josephdelgadillo.com
Alright, so, what I'm going to do is
we're going to move on to for loops. So, a for loop is a way to do something
repetitively until a certain threshold is met. So we're going to
create a variable called doUntil, and this is going to be five. And we're going
to create another variable called, actually that's let's do this all in one.
So, for, and we open this up, we set the initial value so we're going to set i
equal to 0. Put a semicolon and we're going to say this is the condition
that this for loop is going to run until something becomes not true or until this
statement becomes false, so we're going to say i is less than 10, and then put a
semicolon, and then the next one is what's going to happen after the loop is
done. So we're going to say i++ which means we're going to add 1 to the
i variable. Let's create that, there we go. Now I'm going to print out just i, so
let's go ahead and console.log("Current i value is" and I'm going to put it comma,
and I'm going to print out i. Now, I'm going to save this and refresh, and as
you can see it repeats that for as long as this variable, here's the conditional
statement, is less than 10. So, again, what's happening is the first loop
through it's going okay, let's print this, i equals 0, then let's add one to i, and
go back through this. Is 1 less than 10? Yep, so, print that again and it prints out 1.
Let's add another to i so it becomes 2, and it says is 2 less than 10? And it
continues that until this statement becomes
false. So, if I put if this is going to do nothing essentially because we haven't
initialized i, it doesn't even run once. And that's a for loop so you can do
something multiple times, and this is a very basic and essential bit of code
right here. You're going to have to know how to make four loops to repeat
something over and over, because we don't want to repeat things over and over. We
want it to be a little bit flexible and run however many times it has to, so
that's why we do this. And, by the way, you can name the i variable, you can name
that anything you want, but you have to use it consistently here. If you change
this to K, then you need to check K against you know the truth checking, and
then you also need to add 1 to K, not i. So, that is a basic for loop. In the next
video, what I want to get into is maybe putting a few things together and
creating functions.
