Let's dive in into four loops and to show how for loops is easy to work.
Let me give you an example.
I want to print it to the console.
Console.log the word Hi, but I want to print this one five times so the easy way I will do that is
going to be two, three, four and five.
Yeah.
And then I come here high one and then high two and then high three and high four.
And the last one hi fi, very time consuming.
But at least I have the end result.
Try to do that with 100 times.
It's going to be very hard.
Yeah, a thousand, even harder.
So is there any way I can ask the JavaScript to loop between those numbers and start counting those
numbers and give me the end result?
Yes, that is.
And we're going to replace all of these for two lines of code.
This is the for loop.
Okay, So let me save and let's start the for loop.
For loop.
The first statement I need to give the for loop three statements here.
The first one inside of the for loop is the initialization process.
So first I need to initialize a variable.
So let's start a variable.
Let's put a variable over here.
Say variable I i of index can be x can be l can be anything.
It's a normal convention that you normally are always most always most every single time we use I I
is equal to zero.
What I'm saying over here is if you want to loop, we need to count how many loops.
So JavaScript needs a base to where should I start counting?
So we start counting on index zero.
So if you start counting on index zero when the loop is going to stop.
So that's the second statement or the condition, I would say.
Yeah, so it will stop when I is less than five.
I would say less than equal, yeah, less then an equal to five.
So we start in zero, we stop on less than or equal to five.
And what is the increment?
Every time I loop, should I count plus one plus two plus three plus four.
In our loop, I need plus one.
So I plus plus means increment as one I, I minus minus increment decrements in one.
So I'm going to leave as one one OC.
Curly braces and the inside is my statement itself.
Here my statement is console dot log.
And I want the word Hi.
There we go.
Simple like that.
So we have the first statement over here is semicolon and let's run.
So executing and I have six eyes.
Well, I have six eyes.
Can you see those six eyes?
Yeah.
So I have six eyes over here.
Why do I have six highs?
Because it's from index zero to index less than and equal to five if I want to just five, it's just
going to be less than.
Now I have my five highs, but I didn't see the high one, the high two, the high three.
The high for the high five.
Yeah.
I can concatenate that with plus I show me the number.
Let's just get a space over here and execute it again.
High zero one, two, three, four, five.
We are very close.
Very close.
I need to fix something.
Start from one and less than or equal to five.
Now I have the one, two, three, four, five.
So did you see?
How simple is the four for the initialization?
The top where the one is stop or the condition and then the increment.
And down here the statements.
You read the statement and then you execute the for loop.
Very simple.
Pretty straightforward.
But on the next class, I want to show you how can we work with arrays, arrays and for loop at the
same time so we can build our our application.