Loops in javascript

Loops in javascript

  1. for loop:

    1. Single for loop: Below is the syntax of a for loop with the example of skipping the even numbers and printing the odd numbers between 1 to 10.

       for (let i = 0; i <=10; i++) {
           const element = i;
           if (element%2==0) {
               continue
           }
           console.log(element);
       }
      
    2. Nested for loop: Below is an example of printing the table of 1 to 5 using the nested for loop.

      
       for (let i = 1; i <= 5; i++) {
           console.log(`inner loop value: ${i}`);
           for (let j = 1; j <=10 ; j++) {
               console.log(i +'*'+ j +'='+ i*j);
           }
      
       }
      
    3. For loop on array: Below is an example of accessing all the elements of an array using the for loop.

       const myArray = ['Superman','Batman','Flash']
       for (let index = 0; index < myArray.length; index++) {
           const element = myArray[index];
           console.log(element);
       }
       /*Output: Superman
       Batman
       Flash */
      
  2. While loop: Below is the syntax for the while loop with an example of printing the even numbers between 0 to 10.

     let index = 0
     while (index<=10) {
     console.log(`value of index is ${index}`)
     index+=2;   
     }
    
  3. Do-while loop: The do-while loop is similar to the while loop only difference is do-while loop executes at least once as the condition is checked at the end. Below is an example of do-while loop.

     let score=11
     do {
         console.log(`score is ${score}`);
         score++
     } while (score <=10); //do while atleast executes 1 time
    
  4. Array loops: In Javascript, there are some inbuilt loops, particularly for arrays.

    1. For of loop: The demonstration of for of loop on arrays, strings, and maps is as shown below. For this loop, we don't need to give start value, end value, and increment.

       //1. For of loop: 
       //example for array
       const superheroes = ['batman', 'superman','flash']
       for (const heroes of superheroes) {
           console.log(heroes);
       }
       /* Output:
       batman
       superman
       flash
       */
      
       //example for string
       const greetings = "hello world!"
       for (const greet of greetings) {
           console.log(`each character is ${greet}`);
       }
       /* Output:
       each character is h
       each character is e
       each character is l
       each character is l
       each character is o
       each character is  
       each character is w
       each character is o
       each character is r
       each character is l
       each character is d
       each character is !
       */
      
       //example for maps
       const maps = new Map()
       maps.set('IN', 'india')
       maps.set('USA', 'united states of america')
       maps.set('FR', 'france')
       maps.set('IN', 'india')  //maps allows unique entries order remains same 
      
       for (const [key,value] of maps) {
           console.log(key, ':', value);
       }
       /* Output:
       IN : india
       USA : united states of america
       FR : france
       */
      
    2. For each loop: The demonstration of for-each loop on arrays and objects is as shown below. For this loop, we don't need to give start value, end value, and increment.

       const coding = ["js","ruby","java","python","cpp"]
       coding.forEach( function (item)  {
           console.log(item);
       } )
       /* 
       Output:
       js
       ruby
       java
       python
       cpp
       */
      
       //accessing object properties [object values]from an array using forEach
       const myCoding = [
           {
               languageName:"javascript",
               languageFileName: "js"
           },
           {
               languageName:"java",
               languageFileName: "java"
           },
           {
               languageName:"python",
               languageFileName: "py"
           },
       ]
      
       myCoding.forEach( (item) => {
           console.log(item.languageName);
       } )
       /* 
       Output:
       javascript
       java
       python
       */
      
    3. For in loop: The demonstration of for in loop on arrays, ans objects is as shown below. For this loop, we don't need to give start value, end value, and increment.

       const myObject = {
           'game1': 'NFS',
           'game2': 'Spiderman',
           'game3': "GTA5",
           "game4": "COD warzone"
       }
      
       for (const key in myObject) {
           console.log(`${key} : ${myObject[key]} game is available to play`)
       }
       /* Output: 
       game1 : NFS game is available to play
       game2 : Spiderman game is available to play
       game3 : GTA5 game is available to play
       game4 : COD warzone game is available to play
       */
      
       const programming = ["js","ruby","java","cpp","python"]
      
       for (const key in programming) {
          console.log(programming[key]);
       }
       /* Output: 
       js
       ruby
       java
       cpp
       python
       */