Simple Loop In JavaScript Program

Simple For Loop


//  index   0   1   2   3   4
let arr = [23, 12, 53, 34, 54];
// Defined variable arr and initialize value

for (var i = 0; i < arr.length; i++) {
// Defined variable i initialize start value is 0 and
// run up to end of index value in help of  arr.length

  console.log("arr =", [i], arr[i]);
}

Output


arr = [ 0 ] 23
arr = [ 1 ] 12
arr = [ 2 ] 53
arr = [ 3 ] 34
arr = [ 4 ] 54

Simple While Loop


//  index  0   1   2   3   4 
let arr = [23, 12, 53, 34, 54];
// Defined variable arr and initialize value

let j = 0;
// Defined variable j

while (j < arr.length) {
// Defined variable j and run up to end 
// of index value in help of  arr.length

  console.log("arr =", [j], arr[j]);
  j++;
}

Output


arr = [ 0 ] 23
arr = [ 1 ] 12
arr = [ 2 ] 53
arr = [ 3 ] 34
arr = [ 4 ] 54

Simple Do While loop


//  index  0   1   2   3   4 
let arr = [23, 12, 53, 34, 54];
// Defined variable arr and initialize value

let j = 0;
// Defined variable j

do {
  console.log("arr =", [j], arr[j]);
  j++;
} while (j < arr.length);
// run up to end of index value in help of arr.length

Output


arr = [ 0 ] 23
arr = [ 1 ] 12
arr = [ 2 ] 53
arr = [ 3 ] 34
arr = [ 4 ] 54