DEV Community

Mahin Tazuar
Mahin Tazuar

Posted on • Updated on

Javascript With Loop

Loop is a very important thing in every programming language. When we need to do something again and again and also need to control the infinite process. At this time we are use Loop.

Today we apply this in Javascript Language. Javascript have 5 kinds of loop.

  1. for
  2. for in
  3. for of
  4. While
  5. Do while

  6. for loop :
    for loop have a simple syntax, as usually, we need to use define some statement after for loop keyword. normally for loop working with array.

  7. for in loop working with an object. If we need to get object element then we are using this loop.

Ex:

let dt = [
    'mahin','tazuar'
  ]
  for (d of dt){ 
    console.log(d) // mahin ,tazuar
  }
Enter fullscreen mode Exit fullscreen mode
  1. for loop working with an array, It's a simple return array element.
let data = {
      name:'mahin',
      age:34
   }
  for (a in data){ 
    console.log(data[a]) // mahin , 34
  }
Enter fullscreen mode Exit fullscreen mode
  1. That is simple and like for loop
  2. Do while loop, when we need to do something after we are starting our loop in this time we are using do-while loop.
 let data = [1,34,343,5345,4656,56]
  var i = 0;
  do {
      console.log(data[i])
    i++;
  }
  while (i < -1);
Enter fullscreen mode Exit fullscreen mode

do while loop , real life example -
suppose your hand is not clean , so you use do loop and wash your hand. then while loop check the hand if the hand is clean execute the while otherwise loop is start agin.

Top comments (0)