JavaScript Loops

The JavaScript loops are used to iterate the piece of code using for, while, do while or for-in loops. It makes the code compact. It is mostly used in array. It is also called iterative statements.

Advantages of using Loop:

  1. It perform repetitive task with less number of codes.

There are four types of loops in JavaScript.

1) JavaScript For loop

The JavaScript for loop iterates the elements for the fixed number of times. It should be used if number of iteration is known. It executes a block of code until a specified condition is true.

The syntax of for loop is given below.

In the above syntax:

  • Initialize: This is an expression that starts the loop.
  • condition: This is a Boolean expression that specifies whether the loop should execute a loop or not.
  • Increment/Decrement: Executes the iterate (i.e. increment and decrement) after each iteration of the for loop statement.

Let's see the simple example of for loop in javascript.

Test it Now

Output:


Explanation: In the above example, we have created a "for loop" functionality. In this, we have initialized a variable with value of 1 and after each iteration value of variable is incremented by 1. This condition is executed till the condition is less than equal to 5.

2) JavaScript while loop

The JavaScript while loop iterates the elements for the infinite number of times. It should be used if number of iteration is not known. In the while loop, the condition is first checked and if it evaluates to TRUE, the statements inside the curly braces are executed.

The syntax of while loop is given below.

In the above syntax, condition is any valid test expression that may use logical operator that is enclosed within parenthesis.

Let's see the simple example of while loop in javascript.

Test it Now

Output:


Explanation: In the above example, we have created a "while loop" functionality. In this, we have initialized a variable with value of 11 and after each iteration value of variable is incremented by 1. This condition is executed till the condition is less than equal to 15.

3) JavaScript do while loop

The JavaScript do while loop iterates the elements for the infinite number of times like while loop. But, code is executed at least once whether condition is true or false. It is also called an exit controlled looping statement because the test condition is checked at the end of the do-while loop and hence the body is executed unconditionally for the first time.

The syntax of do while loop is given below.

Let's see the simple example of do while loop in javascript.

Test it Now

Output:


Explanation: In the above example, we have created a "do while loop" functionality In this, we have initialized a variable with value of 21 and after each iteration value of variable is incremented by 1. This condition is executed till the condition is less than equal to 25.

4) JavaScript for in loop

The JavaScript for in loop is used to iterate the properties of an object. JavaScript provides a variation of a loop known as a for loop that can be used to extract the names and values of any object. It allows looping through all elements of an array or properties of an object.

The syntax of for in loop is given below.

In the above syntax, v_name is the name of the variable and object_name is the name of the object.

Let's see the simple example of for in loop in javascript.

Output:

Example of For In Loops
75
14
89
16

Explanation: In the above example, we have created a ""For In loop" functionality. In this, we have initialized a constant array and its values are accessed with the help of index.