In this JavaScript For Loop tutorial, we do check the complete working of for loop with the help of different examples. So we do start the complete tutorial step by step,

  • Introduction of JavaScript For Loop
  • Workflow of JavaScript For Loop
  • Different types of For loop with example
  • Conclusion
How JavaScript For Loop Works Complete Guide With Example
How JavaScript For Loop Works Complete Guide With Example

Introduction of JavaScript For Loop

Loop through a block of code a number of times that is defined by users. This means the For loop provides three optional expressions where we define the conditions to run the block of code.

Workflow of JavaScript For Loop

Syntax of JavaScript For Loop

Initialization Here user initialize the value of the loop from where loop starts and it executed one time on the complete loop process.

Condition Here we define the condition when the loop will stop. Means this executes at every iteration and check the condition whether it is true or false. If the defined condition is true then loop iteration will continue, if false then the loop will be terminated.

Condition part of for loop is optional, if you did not define, it always takes true.

post-expression when the loop executes it will help to update the counter variable and the loop will continue.

JS For Loop Work Flow
JS For Loop Work Flow

Example of JS For Loop

Output:

Count 0
Count 1
Count 2
Count 3
Count 4

Code Explanations:

  • In a first statement, we set the variable (var i = 0) before the loop starts.
  • Define the condition when the loop will stop (when the value i must be greater than 5).
  • Then loop will increase the value of i on each execution till the condition will false.

Types of For Loop With Example

Here are 3 major type of for loop or you can say different kind of loops in JS.

  1. For: Used to loop through the code n number of times till the condition is false
  2. For/in: Used to loop through object properties
  3. For/of: Used to loop through an array of iterable objects

The For/In Loop

The For/In loop used to loop through the properties of an object.

Syntax of The For/In Loop

Example of The For/In Loop

Output:

John Developer 25

Code Explained:

  • Here we create an object named person
  • And the For/In loop iterates the person object.
  • Each iteration returns a key 0,1,2.. etc (x)
  • The key is used to access the data of person object (person[x])

The For/In Loop Over Arrays

Example of The For/In Loop Over Arrays

Output:

bikash
John
Sundar

The For/Of Loop

The For/Of Loop is used to loop through the values of an iterable object.

Example of The For/Of Loop

Output:

BMW
SUZUKI
ROYAL

Major Points:

  • variable – For each iteration, the value of the next property is assigned to the variable.
  • Variable can be declared with const, let, or var.
  • iterable – An object that has iterable properties.

Conclusion

Now we completely discuss about How JavaScript For Loop Works, with the help of example and complete explanations of example and also we check the different types of loop which is used for loop through array, loop through array of objects and JavaScript iterate array.

Also Check:

Check for more https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Loops_and_iteration

Happy Coding..!

Was this article helpful?
YesNo

By Bikash

My name is Bikash Kr. Panda. I own and operate PHPCODER.TECH. I am a web Programmer by profession and working on more than 50 projects to date. Currently I am working on the web-based project and all the CMS and frameworks which are based on PHP.

9 thoughts on “How JavaScript For Loop Works (Complete Guide With Example)”

Leave a Reply

Your email address will not be published. Required fields are marked *