loops in javascript

 for loops

their are different type of for loops that we use

simple for loop

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

use example

  let friends = ["Rohan", "Sanjeev", "Deepti", "Pooja", "SkillF"];
    // for (let index = 0; index < friends.length; index++) {
    //     console.log("Hello friend, " + friends[index]);
    // }

for each loop

// friends.forEach(function f(element){
  //console.log("Hello Friend, " + element + " to modern JavaScript");
    // });
 
this will run a function for each element of the array friends

for of loop

// for (element of friends){
console.log("Hello Friend, " + element + " to modern JavaScript again");
    // }

this will also run the given function for all elements of friends.


this how we make a object in js


    let employee = {
        name: "Harry",
        salary: 2,
        channel: "CWH"
    }

for in loop

 for(key in employee){
        console.log(`The ${key} of employee is ${employee[key]}`);
    }

while loops


    // while loop in js
    let i =0;
    while(i<4){
        console.log(`${i} is less than 4`);
        i++;
    }
this is a typical while loop in JS

do while loop

 // do while loop in js
    let j =34;
    do{
    console.log(`${j} is less than 4 and we are inside do while loop`);
        j++;
    }while(j<4);


difference

a while loop will not run if condition is false whereas a do while loop will run once even if the condition is false.





































Comments