Loops in JavaScript

Loops in JavaScript

Loops are used to run the particular code repeatedly one by one. Just like printing the table of 2, 3, 4, 5 till 10th multiples. There are mainly three types of loops but in JavaScript, we have five types of loops are as follows as:

For Loop: - This is the mostly used loop in the software industry projects. In for loop, firstly we initialize the loop variable then in the next after semi-colon we write condition when loop stop and at last, we increment or decrement the loop variable. Let's see how it works first of all it initialize loop variable then check condition if it is true then run console after that increment happens and if condition is false then control goes out of loop. Developers like this loop because it is very easier to write in code as you can see in the JavaScript program given below.

for(let i=0; i<5; i++)
{
    console.log(i);
}        

While Loop: - In While loop first of all the loop variable is initialized then it checks the condition before execution after that if condition is true then execution happens and then after loop variable increases. So, this is called entry-controlled loop.

let i=1;
while(i<5)
{
    console.log(i);
    i++;
}        

Do-While Loop: - In Do-While loop first of all the loop variable is initialized then loop is executed and incremented then it checks the condition after execution after that if condition is true then goes to first line of code that is loop variable initialization and increase it. So, this is called exit-controlled loop.

let i=1;
do
{
    console.log(i);
    i++;
}while(i<5);        

For Each Loop: - Foreach loop is a loop in JavaScript which iterates over each item of the array. It is especially made for array traversal purpose. In foreach loop we pass an anonymous function as a parameter in which we write some code like traversal of elements of an array or doing some operations in each element of an array. In the anonymous function we pass some arguments are the first argument we pass holds the value of each element of array and the second argument we pass holds the index of each element in the array. This is how foreach loop works.

var arr = [1,2,3,4,5]
arr.forEach(
    function(item, index)
    {
        console.log(item);
        console.log(index);
    }
)        

For In Loop: - For in loop is especially made for objects in JavaScript. For in loop is especially used for the traversal of each key and each value of key of object. Here, the variable i is collecting all the keys present in the object so if you want to print keys then simply print i but if you want to print value then you have to print object_name[i].

var obj = {
    name:"Ram",
    age:12,
    class:"IV",
    roll:2367008
}        
for(let i in obj)
{
    console.log(i);
    console.log(obj[i]);
}        

To view or add a comment, sign in

More articles by Amrit Keshari

  • Function in JavaScript

    Function is a block of code which we write to use it when we call the function in future. Function contains some line…

  • Switch Case in JavaScript

    Switch case is another concept of JavaScript in which we evaluate the given expression and control the flow of…

  • Conditionals in JavaScript

    Conditionals in JavaScript is the concept of if-else, else-if and if statements. Here, we learn how to write code so…

  • Console in JavaScript

    Console functions in JavaScript is not a part of the JavaScript, but it is the part of the Window Object which is…

  • Variables in JavaScript

    As we know that variables are the name of the memory location where value is stored. In JavaScript, we declare and…

Others also viewed

Explore content categories