Looping JavaScript arrays in reverse
Learn how to loop through array backwards in reverse order in JavaScript using Array.reverse() function.
The JavaScript array has a function called reverse(). It changes the sequence of elements of the given array and returns the array in reverse sequence. In other words, the last element becomes first and the first element becomes the last.
const colors = ['Red', 'Blue', 'Green']; const reversed = colors.reverse(); console.log('Reversed Colors:', reversed); //Reversed Colors: (3) ["Green", "Blue", "Red"]
To loop through array backwards, we use the reverse() function and use it in a loop.
const colors = ["Red", "Blue", "Green"]; for (const color of colors.reverse()) { console.log(color); } //Green //Blue //Red