Loops in JavaScript-for, do..while, while, for..in, for..of

          Loops in JavaScript is used to execute a single or a block of statements repeatedly until a given condition is true.

1. for loop:

            When you know exactly how many times you want to loop through a block of statements then use for loop instead of while loop.

            Syntax:

                    for(initialization; condition; increment or decrement){
                            //statements
                    }

            Example:

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

           Output: 0 1 2 3 4 


2. do-while loop:

            This loop will execute the block of code at least once, before checking if the condition is true, then it will repeat the loop as long as the condition is true.

              Syntax:  

                    do{
                            //statements
                    }while(condition);


             Example:

                    let i=1;
                    do{
                        System.out.print(i + " ");
                        i++;
                    }while(i<1);

            Output: 1


3. while loop: 

           The while loop executes a block of statements as long as a specified condition is true. 

            Syntax:

                    while(condition){
                            //statements
                    }

           Example:

                    let i = 0;
                    while(i<5){
                            System.out.print(i + " ");
                            i++;
                    }


            Output: 0 1 2 3 4 

Note: If the condition is always true, then the loop is called infinite loop.


4. For-in loop:

        This loop can be used to iterate over an object. It iterates over all the properties or elements of an object. 

       Syntax:

              for(variable in object){
                   //Statement
               }


        Example:

               let arr = [1, 2, 3]
               arr.prop ="hello"
               for(let i in arr){
                     console.log(i) 
               }

        Output: 0 1 2 prop  //notice here array properties are printed, not its value.


        Consider another example:

               let obj ={uid:1618, name:'process'}

               for(let key in obj){

                    console.log(key+':'+obj[key])

               }

         Output: uid:1618    name:process


5. For-of loop:

         You know that for-in loop iterates over the properties of an object, similarly for-of loop iterates over the property values of an object.

           Syntax:

              for(variable of object){
                   //Statement
               }


        Example:

               let arr = [1, 2, 3]
               arr.prop ="hello"
               for(let i of arr){
                     console.log(i) 
               }


        Output: 1 2 3  //It will ignore props which are not valid as an index, like string, float values etc


        Consider another example:

               let obj ={uid:1618, name:'process'}

               for(let i of Object.entries(obj)){

                    console.log( i )

               }


         Output: ['uid',1618] [ 'name','process' ]


Continue Learning: JavaScript Functions - Hoisting and Closures


Comments

Popular Posts