Skip to content
the poor coder

javascript loop through array backwards

Poor Man
Published
Reading time
2 min read
On this page

Javascript Loop Through Array Backwards#

Looping through an array is a common operation when working with arrays in Javascript. Sometimes, we may need to loop through the array backwards instead of forwards. There are different ways to loop through an array backwards in Javascript, we can use a for loop, while loop, or the forEach() method provided by arrays in ES6.

Using a for loop#

We can loop through the array backwards using a for loop by starting the loop with the last index of the array and decrementing it until we reach the first index.

Text

let arr = ["apple", "banana", "orange", "grape"];
for(let i = arr.length - 1; i >= 0; i--) {
  console.log(arr[i]);
}

In this example, we are looping through the array 'arr' backwards using a for loop. We start the loop with the last index of the array, which is 'arr.length - 1', and decrement 'i' until we reach the first index, which is 0. Inside the loop, we access each element of the array using the index 'i' and log it to the console.

Using a while loop#

We can also use a while loop to iterate through the array backwards. We start with the last index of the array and decrement it until we reach the first index.

Text

let arr = ["apple", "banana", "orange", "grape"];
let i = arr.length - 1;
while(i >= 0) {
  console.log(arr[i]);
  i--;
}

In this example, we are using a while loop to iterate through the array 'arr' backwards. We start with the last index of the array, which is 'arr.length - 1', and decrement 'i' until we reach the first index, which is 0. Inside the loop, we access each element of the array using the index 'i' and log it to the console.

Using forEach() method#

In ES6, arrays come with a built-in forEach() method that allows us to loop through each element of the array. However, the forEach() method only iterates through the array in the forward direction. To loop through the array backwards using forEach() method, we can first reverse the array and then use forEach() method to iterate through it.

Text

let arr = ["apple", "banana", "orange", "grape"];
arr.reverse().forEach(function(item) {
  console.log(item);
});

In this example, we are reversing the array 'arr' using reverse() method and then using forEach() method to iterate through it. Inside the loop, we access each element of the array using the argument 'item' and log it to the console.

These are some of the ways to loop through an array backwards in Javascript. Depending on the situation, we can choose the appropriate method that suits our needs.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC