If you already have a subscription, you can sign in.
Enjoy free content straight from your inbox 💌
00:00
For most looping needs In JavaScript, you will most likely use the for loop. But when you are getting values only on demand, then the while Loop is an excellent candidate. Consider the simple array consisting of people, Jane, John, Jack, and Joe. If you want to log out the names of all of these people, we can do that quite easily. With an index based for Loop, we can actually achieve the same effect with the while loop provided by JavaScript. The while loop is syntactically much simpler than a fall loop because it only consists of a condition and a while loop body. It starts off by first checking the condition. If the condition is true,
00:34
it's going to execute the body and then it's going to check the condition again. And as long as the condition is going to be true, the body is going to execute for another iteration. Unlike the fall loop, we are responsible for doing our own initialization, which we are doing with the simple statement before the Y loop. And we are responsible for our own afterthought, which we are doing as an additional statement in the body of the Y loop. There is a slight advantage of using a fall loop here in that it doesn't leak its initialization variable to the after scope. However, the Y loop does, which can result in variable conflicts with other loops. However, with the Y loop,
01:07
you do have more control in how you want to structure your initialization and the afterthought. A strong reason for using the Y loop is when your action is going to have an impact on the items that you are iterating over. As an example, consider wanting to remove the person one by one from the people array and then logging it out. This is quite easy to do with a Y loop because we simply pop the last item, store it in a person variable and then log it out. This sort of thing is not a great experience for a fall loop, as you really shouldn't use a fall loop and the item you are iterating over might change. Once this while loop terminates,
01:40
the people array should be empty and we can verify that with a simple log statement. And when we run this code, you can see that it matches our expectations. Sometimes you want to execute the body of the while loop at least once, irrespective of the condition being true. And for that, we can use the do while loop. Consider the example where we want to log out the Fibonacci numbers, which are less than 100. We know that the first Fibonacci number is zero and the second Fibonacci number is one. Since the initial Fibonacci number is less than 100, there is no point in checking if it is less than 100 upfront. So we use a DOA loop within the body. We log out the current Fibonacci number,
02:17
and then we move the next Fibonacci number, which is current two into current one, and calculate the next upcoming by simply adding current one plus current two. Now, we will repeat this process as long as the current Fibonacci number is less than 100, and for that we add the vial with the condition. That current one should be less than 100. And if you run this code, you can see that it works as expected. We start off with zero than one and then continue till current one becomes greater than 100. I hope you enjoyed this short focus tutorial. As always, thank you for joining me and I will see you in the next one.