JavaScript Loops

sometimes we want to repeat an action a number of times for example let's imagine we want to display hello world five times on the console the poor way of doing that is like this so a console or log hello world and then repeated five times this code is ugly there is a better way to achieve the same result and that's where we use loops in JavaScript we have various kinds of loops and all these loops essentially do the same thing to repeat an action a number of times we have four loops while loops do-while loops 4 in loops and for off loops all these loops essentially do the same thing but there is a subtle difference between how they start and end let's start by looking at the for loop so back to this code this is how we write a for loop so for we add parentheses now here we need three statements the first statement is what we call the initial expression and here we declare and initialize a variable so we use let to declare a variable like I and set it to 0 now I is short for index and is a common convention to use in for loops this is what we call the loop variable so we initialize it to 0 then we terminate this statement with a semicolon now the second part of the for loop is what we call a condition so here we want to add a condition and compare the value of I with something else this loop will run as long as this condition evaluates to true so if you want this look to run 5 times we compare I with 5 so as long as I is less than 5 this loop will execute you will see that in a second once again we terminate this statement with a semicolon and finally the third part this is what we call increment expression and quite often what we have here is something like this so we use the increment operator to increment the value of I with 1 now after this for statement we can one or more statements just like the if statements if you have multiple statements here we need to put them in a code block like this now here we want to display hello world on the console so our statement is console that log hello world so instead of repeating this line five times we put it in a for loop and this loop will run five times now save the changes so we get five hello world messages on the console alright now that you have seen a for loop in action let's see exactly how this loop works so earlier I told you this is what we call an initial expression here we are initializing I to 0 now this loop will execute as long as this condition is true so as long as I is less than 5 the statements we have here will be executed now after the first iteration I is incremented by 1 then this condition is evaluated again so 1 is less than 5 so one more time this loop is executed so we have the second iteration now after the second iteration once again is incremented by 1 so now we are in the third iteration again this condition is evaluated and because it is true the loop is executed to show you this in action I'm gonna output I on the console so save the changes this is what we get so note that in the first iteration I is 0 then is incremented by 1 until it reaches 4 so at the end of fifth iteration I will before and when we increment that by 1 it will be 5 so this condition will evaluate to false so essentially there are two ways to repeat an action a number of times using the for loop let's say we want to repeat something 5 times we can initialize I to 0 and check to see if it's less than 5 alternatively we can initialize this to 1 and check to see if it's less than or equal to 5 now if you save the changes we see I starts from one and finishes at five now we can make this program a bit more interesting let's say we want to display the odd numbers between one to five so instead of log in hello world on the console we can have an if statement and check the remainder of the division of I by two so if the remainder of division of I by 2 is not 0 that means I is an odd number so we can display it on the console save the changes so here are the odd numbers between 1 to 5 there is also another way to write this loop instead of starting from 1 and going all the way up to 5 we can start from 5 and go back to 1 so we change the initial expression set I to 5 as long as I is greater than or equal to 1 now we want to determine I save the changes now we get the odd numbers in the reverse order it's more common to use the previous form so we initialize I to 0 or 1 and increment it in every iteration but in certain problems you want to use a for loop in the reverse order hi guys thank you for watching my javascript tutorial this tutorial is part of my JavaScript course where you learn all the essential JavaScript features that every web and mobile application developer must know if you're an absolute beginner or have some experience in JavaScript and are looking for a fun and in-depth course that teaches you the fundamentals of JavaScript this course is for you this course is also packed with tons of exercises that help you master what you learned in the course in fact many of these exercises are questions that come up in technical programming interviews so if you're pursuing a job as a front-end or a back-end developer or if you simply want to have a more in-depth understanding of JavaScript I highly encourage you to enroll in the course for a limited time you can get this course with a discount using the link in the video description click the link to find out more about the course and enroll

Loading