in

JavaScript For loop | Understanding the Use of The For Loop

In this tutorial we will be looking at the JavaScript for loop which entails how to make use of the for loop when coding in JavaScript. In our previous tutorial we looked at JavaScript while loop and we explained how to use the while loop for the right purpose. If you haven’t gone through the tutorial you can do so using the link below

Read previous tutorial on: JavaScript While loop | Understanding the Use of The While Loop

JavaScript For  Loop

The For Loop can be used to carry out a repeated action over and over again for a certain number of times. This loop is similar to other loops but its structure is slightly different from other loops.

The For loop is the most compact loop Which contains the three important parts needed for looping and they include:

The initialization: The For loop unlike other loops allows the initialization of the counter to a first value which then executes itself before the loop begins

The test statement: The For loop allows for testing of the condition to confirm if the statement is valid. if the statement is valid the loop executes and if its not valid the loop doesn’t execute.

The iteration condition: The For loop gives allowance for increase and decrease of the counter.

The syntax for the For loop is shown below

for (The first initialization; test conditional statement; iteration statement) {
   The statement to be executed if the condition is true
}

The For Loop Flow Chart

EXAMPLE

<!DOCTYPE html>
<html lang="en">
<head>
   <title>THE FOR LOOP </title>
</head>
<body>
   <script>
      var count = 0;
      for (count=0; count <10; count++ ){
         document.write("counting " + count + "</br>");
      }
   </script>
   
</body>
</html>

RESULT

THE FOR LOOP

CONCLUSION

The JavaScript For loop is very important and can be used to replace some other loops. We hope with this tutorial you clearly understand how to use the FOR loop and also how to draw the For loop flow chart.

NEXT TUTORIAL: JAVASCRIPT FOR-IN-LOOP

What do you think?

javascript while loop

JavaScript While loop | Understanding the Use of The While Loop

javascript for in loop

JavaScript For in loop | Understanding the Use of The For in Loop