If you want an equal interval, your loop is fine, you just need to fix the math for calculating the timeout:

const btn = document.getElementById('btn');
btn.addEventListener('click', function(){
  
    for (let index = 10; index > 0; index--) {
        setTimeout(() => {
            console.log(index)
        },  1000*(10-index));      
    }
})
<div class="app">
  <button id="btn">click</button>
</div>

That will count down from 10 to 1 once every second, just change the 1000 as needed if you want it to be faster or slower.

Note that the order of the loop doesn't actually matter here (aside from a few ms difference in execution time) since it's asynchronous - you could use a non-reversed loop and still get the same result to the human eye.

Answer from John Montgomery on Stack Overflow
Top answer
1 of 2
3

If you want an equal interval, your loop is fine, you just need to fix the math for calculating the timeout:

const btn = document.getElementById('btn');
btn.addEventListener('click', function(){
  
    for (let index = 10; index > 0; index--) {
        setTimeout(() => {
            console.log(index)
        },  1000*(10-index));      
    }
})
<div class="app">
  <button id="btn">click</button>
</div>

That will count down from 10 to 1 once every second, just change the 1000 as needed if you want it to be faster or slower.

Note that the order of the loop doesn't actually matter here (aside from a few ms difference in execution time) since it's asynchronous - you could use a non-reversed loop and still get the same result to the human eye.

2 of 2
1

If you use promises and async/await you can avoid the setTimeout overload and have precise control. The difference between this and John Montgomery's answer is this doesn't care how many times you loop. Do 10000 if you want, you won't get 10000 setTimeouts running at the same time.

const btn = document.getElementById('btn');
const wait = time=>new Promise(resolve=>setTimeout(resolve,time))
btn.addEventListener('click', async function(){      
    for (let index = 10; index > 0; index--) {
        await wait(1000)
        console.log(index)
    }
})
<div class="app">
  <button id="btn">click</button>
</div>

Async functions allow you to use the await keyword. This is a neat new feature in javascript that let's you write async code (like setTimout) as if it was sync.

const wait = time=>new Promise(resolve=>setTimeout(resolve,time)) is a fairly common helper function developers keep around for times you need to just wait some amount of time and then do something. By await'ing the wait(1000) function, I hang the execution of the loop until the time is done.

🌐
AlgoCademy
algocademy.com › link
Looping In Reverse in JavaScript | AlgoCademy
In this lesson, we will explore how to use a for loop to count backwards in JavaScript. Looping in reverse is a common requirement in programming, especially when you need to process elements in reverse order or when decrementing values.
Discussions

javascript - Are loops really faster in reverse? - Stack Overflow
Are JavaScript loops really faster when counting backward? If so, why? I've seen a few test suite examples showing that reversed loops are quicker, but I can't find any explanation as to why! I'm assuming it's because the loop no longer has to evaluate a property each time it checks to see if it's finished and it just checks against the final numeric value. ... for ... More on stackoverflow.com
🌐 stackoverflow.com
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
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
Best way to loop back and forth though an array?
Assuming you have some index to track where you are in the array, to make it wrap use index = (index + array.length) % array.length So if there are 3 items in the array and index is -1, you'll get (-1 + 3) % 3 = 2 // last index or if you reach past the end at index 3 (3 + 3) % 3 = 0 // back to start More on reddit.com
🌐 r/learnjavascript
5
1
August 26, 2022
🌐
DEV Community
dev.to › tpointtech123 › how-to-reverse-a-string-in-javascript-using-a-for-loop-1aof
How to Reverse a String in JavaScript Using a For Loop - DEV Community
March 20, 2025 - Here’s the general approach: Create an empty string that will hold the reversed string. Loop through the original string from the last character to the first. Append each character to the new string in reverse order.
Find elsewhere
🌐
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   July 23, 2025
🌐
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 :/

🌐
TutorialsTonight
tutorialstonight.com › reverse-for-loop-javascript
Reverse for Loop Javascript (with Examples)
April 23, 2021 - Here, we have used the length property of a string to get the last index of the string and then decreased the index to move backward. ... This is all about for loop in reverse order in javascript.
🌐
Log4JavaScript
log4javascript.org › home › js-framework › mastering backward iteration with for loops in javascript
Reverse Number in JavaScript Using For Loop: A Guide
October 24, 2023 - Attention: It’s important to note that when running a backward loop, initiating from the loop’s terminal point and modifying the breaking criteria are both necessary adjustments. Let’s illustrate this by decrementing integers from 5 to 1. // Backward iteration from 5 to 1 for (let counter = 5; counter > 0; counter--) { console.log(counter); }
🌐
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 …
🌐
GitHub
github.com › EQuimper › CodeChallenge › blob › master › javascript › FreeCodeCamps › Basic JavaScript › Count Backwards With a For Loop.md
CodeChallenge/javascript/FreeCodeCamps/Basic JavaScript/Count Backwards With a For Loop.md at master · EQuimper/CodeChallenge
February 16, 2021 - In order to count backwards by twos, we'll need to change our initialization, condition, and final-expression. We'll start at i = 10 and loop while i > 0. We'll decrement i by 2 each loop with i -= 2. var ourArray = []; for (var i=10; i > 0; ...
Author   EQuimper
🌐
IQCode
iqcode.com › code › javascript › how-to-reverse-loop-in-javascript
how to reverse loop in javascript Code Example
October 8, 2021 - Sign up · Develop soft skills on BrainApps Complete the IQ Test ... backwards loop js reverse an array function in loop reverse an array function in js loop reverse an array loop javascript reverse array with loop reverse while loop in javascript how to reverse items in a array in javascipt with a for loop javascript go throue an array backwards loop over array backwards javascript while loop backwards js iterate an array backwards how to loop over an array backwards javascript Using a for loop output the elements in reverse order js run for loop in reverse javascript run a reverse loop on ar
🌐
Bobby Hadz
bobbyhadz.com › blog › javascript-loop-object-reverse-order
Loop through Object in Reverse Order using JavaScript | bobbyhadz
Copied!const obj = { a: 'one', b: 'two', c: 'three', }; // 👇️ ['c', 'b', 'a'] const reversedKeys = Object.keys(obj).reverse(); reversedKeys.forEach(key => { console.log(key, obj[key]); // 👉️ c three, b two, a one }); ... The first step is to get an array of the object's keys by using the Object.keys() method. ... Copied!const obj = { a: 'one', b: 'two', c: 'three', }; const keys = Object.keys(obj); console.log(keys); // 👉️ ['a', 'b','c'] The Object.keys() method returns the object's keys ordered in the same way as given by looping over the object's properties manually.
🌐
HTML Goodies
htmlgoodies.com › home › guides
The Advantages of Backwards Iteration as Demonstrated in JavaScript | HTML Goodies
October 8, 2021 - While there are staunch proponents for both sides, trying to save a few cycles within a loop is neither here nor there, unless you are dealing with hundreds of thousands of items, and, if you are working with arrays that large, why the heck are you doing that in JavaScript?! The purpose of this tutorial is not to put every loop type to the test. Rather, I’d like to describe what kind of scenario might logically benefit from backwards ...
🌐
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 - You can use the reverse method, which is an easier-to-read/write approach to the for loop, to reverse an array.
🌐
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.