Iterate Over an Array in Reverse Order in JavaScript

There are various ways to iterate over an array in reverse order:

Using a for loop:

let arr = [1, 2, 3, 4, 5]

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

Using the Array.forEach() method together with the Array.reverse() method. To avoid mutating the original array we need to first create a copy (Array.slice()) and then use the forEach method on that copy:

let arr = [1, 2, 3, 4, 5]

arr.slice().reverse().forEach(val => console.log(val))

// 5, 4, 3, 2, 1

Alternatively we can also use the Object.keys() method to obtain an array with the index values of the array we want to iterate over. We reverse the array containing the index values and use that to access the array in reverse order:

let arr = [1, 2, 3, 4, 5]

Object.keys(arr).reverse().forEach(i => console.log(arr[i]))

// 5, 4, 3, 2, 1