JavaScript Loops

Other Languages like JAVA, PHP, in the JavaScript loops are used for iteration to the piece of code using different loops like For loop, while loop or do-while loop etc. It create easiness in the programm. It is mostly used in array.
There are four types of loops in JavaScript.
1- for loop
2- while loop
3- do-while loop
4- for-in loop

Example of For Loop

In this example syntax of JavaScript For Loop is described. The JavaScript for loop iterates the elements for the fixed number of times. It should be used if number of iteration is known. The syntax of for loop is given below.

Syntax
for (initialization; condition; increment)
{
code to be executed
}

<script type = "text/javascript">
for (i=1; i<=5; i++)  
{  
document.write(i + "
") } </script>

Output of above code

Explanation

There are basically three statements in For loop.
Statement 1
This initial state is executed (one time) before the execution of the code block in JavaScript.
Statement 1 sets a variable before the loop starts (var i = 0) in the above example.

Statement 2
This conditional state defines the condition for executing the code block in JavaScript.
Statement 2 defines the condition for the loop to run (i must be less than 5) in the above example.

Statement 3
This increment/decrement state is executed (every time) after the code block has been executed in JavaScript.
Statement 3 increases a value (i++) each time the code block in the loop has been executed in the above example.


Note:

The while loop, do-while and for/in loop in next chapter.








The Best

Comment here

If you have any query, if you want to know something about any of technical course related to computer science field, if you have any suggestion about relevant to uploaded content or if you anything wrong here (any mistake in content) than please contact us. Keep in mind, comment should be according to community guidelines.