let arr = [1, 2, 3];

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

will print:

3
2
1

arr will still be [1, 2, 3], the .slice() creates a shallow copy.

Answer from naartjie on Stack Overflow
🌐
Reddit
reddit.com › r/learnjavascript › best way to loop back and forth though an array?
r/learnjavascript on Reddit: Best way to loop back and forth though an array?
August 26, 2022 -

Hi, I've got a modal I've made on a site but I would like to have a functional image gallery that goes back and forth though a list of local images no matter that size of the array. My problem is I have the modal, images and button events set but the function messes up as it get to the end of the array of images or the start or when i'm moving forward/backwards then press the reverse as I have to pres the button twice for it to work .

I'd post the code but it's so messed up I'd rather know the best clean solution for this issue.

so

  1. I need the list of images thats in the array to loop as it get to the end/start of the list (using a previous and next button).

1a. As it get to the end of the list. 1b. As it get to the start needed to go back to the end.

I've delayed coding for a while as its frustrated me please help :/

Discussions

javascript - How do i loop through an array backwards? - Stack Overflow
i am attempting to make snake in java script using p5.js and have the snake as an array where the head is the first index and the tail every spot after. i am trying to loop through the array backwa... More on stackoverflow.com
🌐 stackoverflow.com
javascript - Why is iterating through an array backwards faster than forwards - Stack Overflow
Note that your backwards case should start at arr.length-1 and have i >= 0 rather than i > 0. ... The title is misleading. It is no longer the case that directly using arr.length is slower in advanced browsers. ... possible duplicate of JavaScript loop performance - Why is to decrement the iterator toward 0 faster than incrementing ... Because your forwards-condition has to receive the length property of your array ... More on stackoverflow.com
🌐 stackoverflow.com
For loop backwards
The instructions state the following… We need to make three changes to our for loop: Edit the start condition (var i = 0), to set i equal to the length of the vacationSpots array. Then, set the stop condition ( i < vacationSpots.length) to stop when i is greater than or equal to 0. Finally, ... More on discuss.codecademy.com
🌐 discuss.codecademy.com
1
0
October 10, 2017
javascript - How to iterate backwards and forwards through an array? - Stack Overflow
So the aim is to have a loop that can go backwards and forwards through the array. I want to have a for loop that iterates through the array. More on stackoverflow.com
🌐 stackoverflow.com
🌐
AlgoCademy
algocademy.com › link
Looping In Reverse in JavaScript | AlgoCademy
This code iterates over an array in reverse order, printing each element. Here is the complete code implementation for the assignment: ... Use console.log: Print the loop variable at each iteration to verify its value.
🌐
YouTube
youtube.com › watch
JavaScript for Beginners #40 Looping Backwards - YouTube
Here, we will learn how to loop backwards with a for loop. This may be useful if want to loop through an array backwards. 0:00 Looping through a for loop bac...
Published   February 16, 2021
🌐
Medium
medium.com › @chanakyabhardwaj › es6-reverse-iterable-for-an-array-5dae91c02904
ES6 — Reverse Iterable for an Array | by Cha | Medium
July 15, 2017 - ES6 — Reverse Iterable for an Array To iterate over an array, a for..of loop can be used like below: let arr = [1,2,3,4,5]; for (let i of arr) { console.log(i); //outputs 1,2,3,4,5 } But what …
Find elsewhere
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array › reverse
Array.prototype.reverse() - JavaScript - MDN Web Docs
const array = ["one", "two", "three"]; console.log("array:", array); // Expected output: "array:" Array ["one", "two", "three"] const reversed = array.reverse(); console.log("reversed:", reversed); // Expected output: "reversed:" Array ["three", "two", "one"] // Careful: reverse is destructive ...
🌐
xjavascript
xjavascript.com › blog › javascript-loop-through-array-backwards-with-foreach
How to Loop Backwards Through a JavaScript Array Using forEach Without Reversing It — xjavascript.com
Looping backwards through an array with forEach is simple once you master the reverse index formula: array.length - 1 - index. This method avoids mutation, saves memory, and aligns with functional programming patterns.
🌐
freeCodeCamp
freecodecamp.org › news › how-to-reverse-an-array-in-javascript-js-reverse-function
How to Reverse an Array in JavaScript – JS .reverse() Function
November 29, 2022 - By using a for loop, we start looping ... to reversedArray. But there's an easier way to reverse an array, which is using the reverse method....
🌐
30 Seconds of Code
30secondsofcode.org › home › javascript › array › iterate over array in reverse
Iterate over a JavaScript array from right to left - 30 seconds of code
October 10, 2023 - Finally, we can use Array.prototype.forEach() to iterate over the reversed array. const forEachRight = (arr, callback) => arr.slice().reverse().forEach(callback); forEachRight([1, 2, 3, 4], val => console.log(val)); // '4', '3', '2', '1' ... ...
🌐
IQCode
iqcode.com › code › javascript › how-to-reverse-loop-in-javascript
how to reverse loop in javascript Code Example
October 8, 2021 - pt for loop reverse array reverse array loop javascript how do you start a loop from the backwards in javascript how to reverse an array in javascript using for of loop traverse an array backwards iterate through array backwards javascript array loop by reverse es6 reverse loop\ for loop to reverse array get the reverse of an array javascript using loop loop an array backwards javascript iterate backwards through array js reverse array using loop how to iterate backwards in javascript for loop go backwards for loop array javascript reverse through an array for loop js pseudp reverse through an
🌐
DEV Community
dev.to › sh20raj › for-loop-through-an-array-in-backward-direction-in-javascript-421i
For Loop through an array in backward direction in JavaScript - DEV Community
January 18, 2022 - 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.
🌐
TutorialsTonight
tutorialstonight.com › reverse-for-loop-javascript
Reverse for Loop Javascript (with Examples)
Here, we have used the length property of an array to get the last index of the array, and then we have used the decrement operator to move backward. Let's print characters of a string in reverse order using for loop.