Loops are handy when you want to run the same code repeatedly with different values in each execution. There are 3 types of loops in JS:
for () loops → these loops contain 3 statements: the first is the initial expression, executed before the loop begins; the second is the condition that runs the loop; the third is the increment with each execution of the loop. If the second statement in a loop returns true, then the loop will begin again with the incremented value. If it runs false, the loop will end. A loop can also be ended using break, otherwise the loop will run forever if it can.
while () loops → these loops are used to repeat a block of code over and over until a certain condition (enclosed in the parentheses) is met. These are useful when you don’t know the exact amount of times you want the code to be repeated. The result of this expression will always be a Boolean value.
do…while () loops → this is a variation of the previous loop, except in this instance, the code is executed once before testing the condition. The loop will continue repeating as long as the condition is true.
Leave a Reply
You must be logged in to post a comment.