When using a for loop in JavaScript, you must include three parts and they need to be separated by semicolons as well.
-
Initialization: This part is used to initialize a counter variable before the loop starts and it is executed once before the loop starts
-
Condition: This part is needed to specify a condition that is checked before each repetition of the loop and if the condition is true then the loop will continue to run. However if the condition is false then the loop stops running.
-
Iteration: This part is used to increment or decrement the counter variable after each repetition of the loop and occurs at the end of each loop repetition.
To summarize, the three loops in JavaScript are initialization, condition and iteration to need to be separated by semicolons.
Hi Ryan, thanks for your post! I think the part I have most difficulty with is the initialization. It can be difficult to know what variable to use as the counter. We have seen some examples where the variable is i, but others where we need to use a variable that has already been declared, or a variable that shows up in other areas of the code. Do you have any tricks for figuring this out?