We talked about the first other for loop form, which is for in which is used for objects. Today we are going to talk about another form, which is called four of which is used for a race. Here's an example. Suppose that we have an array of employees. We have four elements, john, Bob, Alice, and Kate. And we want to loop through these elements and get all of these elements in order to use them in other parts of our application, or just print them on the screen.
So how would we do so? Actually, there are other There are two ways. The first way is to use the for loop, the simple for loop that we learned about before and the other way is using for that we are going to learn Today, Let's now check both options. So now suppose that we have this array called employees. And we want to loop through these elements. There are two options.
The first one is to use the simple for loop that we learned about before, which is for and then parentheses, and then create let i equals to zero, and then semicolon. And then our condition, which is I is smaller than the length of this array, and the length of this array is four, and we can just say four or we use employees employees dot length, links, and this is a function th and this is a function that will return the number of elements in this array, and then semicolon and then i plus plus. And what that means is that we are going to loop through this array Until if i is greater than the employee links, we are going to get out of this loop. So now what what I would do is just curly braces and just use console dot log, and I'm past just the employees, and then square brackets and then I.
And now if I save and run our program, as you can see, I get john Bob, Alice Mk. And this is the first option by using the simple for loop that can be used for any program. But the one that we are going to learn about today is called four of so in order to use the four off, what you would do is I'm going to delete all of that or comment that out. And I'm going to create for another four of loop which is four, and then parentheses. And then inside it what you would do is you just create a variable called let me ploy II, just the singular, the single the singular form of this array, which is employee instead of employees and then have and then employees, which is this array. And then you could you just add curly braces and inside these curly braces, you use the console log here, console log console dot log, and then you just print out the employee.
So here employee the singular for if now I save and run, I get john, Bob, Alice and Kate. And as you can see, both will do the same job. But it's not usually used we usually use the simple form which is or the conventional form, which is this one, but this one is, this one can also be used, but it's it's being used much less than the simple four So to recap in order to use in order to loop through an array, we have two options. Either we have the simple four or we can use the for loop.