loop in reverse - 01

Looping in reverse:

If we want a for loop to print or log in a reverse direction like 3, 2, 1, 0. 

To run backward for loop, we must ...

  • We have to set the iterator variable to the highest value in the initialization expression.
  • Set the lowest value or stoping value for the condition or
  • Set the decrement to decrease the value of the final expression.

const arr = [1, 2, 3, 4, 5, 6, 7];
  for (let i = arr.length; i >= 0; i--) {
    console.log(arr[i]);
}

Post a Comment

Previous Post Next Post