For Loop through an array in backward direction in JavaScript

There are several methods to loop through the array in JavaScript in the reverse direction:

1. Using reverse for-loop

The standard approach is to loop backward using a for-loop starting from the end of the array towards the beginning of the…


This content originally appeared on DEV Community and was authored by Shaswat Raj

There are several methods to loop through the array in JavaScript in the reverse direction:

1. Using reverse for-loop

The standard approach is to loop backward using a for-loop starting from the end of the array towards the beginning of the array.

var arr = [1, 2, 3, 4, 5];

for (var i = arr.length - 1; i >= 0; i--) {
    console.log(arr[i]);
}

2. Using Array.prototype.reverse() function

We know that forEach goes through the array in the forward direction. To loop through an array backward using the forEach method, we have to reverse the array. To avoid modifying the original array, first create a copy of the array, reverse the copy, and then use forEach on it. The array copy can be done using slicing or ES6 Spread operator.

var arr = [1, 2, 3, 4, 5];

arr.slice().reverse()
    .forEach(function(item) {
            console.log(item);
});

Alternatively, you can use the Object.keys() method to get keys:

var arr = [1, 2, 3, 4, 5];

Object.keys(arr).reverse()
        .forEach(function(index) {
            console.log(arr[index]);
});

3. Using Array.prototype.reduceRight() function

The reduceRight() method executes the callback function once for each element present in the array, from right-to-left. The following code example shows how to implement this.

var arr = [1, 2, 3, 4, 5];

arr.reduceRight((_, item) => console.log(item), null);

That’s all about looping through an array backward in JavaScript.

Source :- https://www.techiedelight.com/loop-through-array-backwards-javascript/


This content originally appeared on DEV Community and was authored by Shaswat Raj


Print Share Comment Cite Upload Translate Updates
APA

Shaswat Raj | Sciencx (2022-01-18T07:35:20+00:00) For Loop through an array in backward direction in JavaScript. Retrieved from https://www.scien.cx/2022/01/18/for-loop-through-an-array-in-backward-direction-in-javascript/

MLA
" » For Loop through an array in backward direction in JavaScript." Shaswat Raj | Sciencx - Tuesday January 18, 2022, https://www.scien.cx/2022/01/18/for-loop-through-an-array-in-backward-direction-in-javascript/
HARVARD
Shaswat Raj | Sciencx Tuesday January 18, 2022 » For Loop through an array in backward direction in JavaScript., viewed ,<https://www.scien.cx/2022/01/18/for-loop-through-an-array-in-backward-direction-in-javascript/>
VANCOUVER
Shaswat Raj | Sciencx - » For Loop through an array in backward direction in JavaScript. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/01/18/for-loop-through-an-array-in-backward-direction-in-javascript/
CHICAGO
" » For Loop through an array in backward direction in JavaScript." Shaswat Raj | Sciencx - Accessed . https://www.scien.cx/2022/01/18/for-loop-through-an-array-in-backward-direction-in-javascript/
IEEE
" » For Loop through an array in backward direction in JavaScript." Shaswat Raj | Sciencx [Online]. Available: https://www.scien.cx/2022/01/18/for-loop-through-an-array-in-backward-direction-in-javascript/. [Accessed: ]
rf:citation
» For Loop through an array in backward direction in JavaScript | Shaswat Raj | Sciencx | https://www.scien.cx/2022/01/18/for-loop-through-an-array-in-backward-direction-in-javascript/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.