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.
๐ŸŒ
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 - Append each character to the new string in reverse order. Return the newly constructed string. Letโ€™s implement this using a for loop in JavaScript.
๐ŸŒ
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 Dillion Megida In this article, I'll show you two ways to reverse arrays in JavaScript. The reverse method of arrays reverses an array by making the last item the first, and making the first item the last. The other items in between also ...
๐ŸŒ
TutorialsTonight
tutorialstonight.com โ€บ reverse-for-loop-javascript
Reverse for Loop Javascript (with Examples)
The secret of moving forward or backward using in for loop lies in its definition. ... To reverse use the decrement step instead of the increment step.
Find elsewhere
๐ŸŒ
Inspector
inspector.dev โ€บ home โ€บ how to reverse a string in javascript โ€“ fast tips
How to reverse a string in Javascript - Fast tips - Inspector.dev
November 7, 2024 - let original = "Hello World"; let reversed = ""; for (let i = original.length - 1; i >= 0; i--) { reversed += original[i]; } console.log(reversed); In this example a for loop is used to go through the original string, beginning with the final ...
๐ŸŒ
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 - Learn the art of executing for loops in the opposite direction in JavaScript. This in-depth guide offers multiple illustrations, best practices, and use cases.
๐ŸŒ
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.
๐ŸŒ
Sololearn
sololearn.com โ€บ en โ€บ Discuss โ€บ 981326 โ€บ how-to-reverse-a-number-in-a-for-loop
How to reverse a number in a FOR loop | Sololearn: Learn to code for FREE!
January 5, 2018 - ... Thanks everyone - I did not ... correct output: function reverse(num){ var reversedNum = 0; for(var i = num.length - 1; i >= 0; i--){ reversedNum = reversedNum + num[i]; } return parseInt(reversedNum); } console.log(reve...
๐ŸŒ
Medium
medium.com โ€บ @chanakyabhardwaj โ€บ es6-reverse-iterable-for-an-array-5dae91c02904
ES6 โ€” Reverse Iterable for an Array | by Cha | Medium
July 15, 2017 - let arr = [1,2,3,4,5]; let index = arr.length;//Custom Iterator let reversedIterator = { next : function () { index--; return { done : index < 0, value : arr[index] } } }//Make it an Iterable reversedIterator[Symbol.iterator] = function() { return this; }for (let i of reversedIterator) { console.log(i); //outputs 5,4,3,2,1 }
๐ŸŒ
Chidrestechtutorials
chidrestechtutorials.com โ€บ web โ€บ javascript โ€บ reverse-for-loop.html
JavaScript reverse for loop
JavaScript reverse for loop to display the multiplication table in reverse order: for(var i=10; i>=1; i--) { document.write(2," x ",i," = ",2*i,"<br/>"); }
๐ŸŒ
Code Highlights
code-hl.com โ€บ home โ€บ javascript โ€บ tutorials
7 Powerful Tips for JavaScript Reverse For Loop Mastery | Code Highlights
September 11, 2024 - This example shows how to modify elements of an array while looping in reverse. The output will be [10, 8, 6, 4, 2]. ... In this case, we build a new string by adding characters in reverse order. ... This example shows how to iterate over object properties in reverse order. Most modern web browsers support the JavaScript reverse for loop.
๐ŸŒ
IQCode
iqcode.com โ€บ code โ€บ javascript โ€บ how-to-reverse-loop-in-javascript
how to reverse loop in javascript Code Example
October 8, 2021 - var arr = [1, 2, 3, 4, 5]; for (var i = arr.length - 1; i &gt;= 0; i--) { console.log(arr[i]); }
๐ŸŒ
W3Schools
w3schools.com โ€บ jsref โ€บ jsref_reverse.asp
JavaScript Array reverse() Method
The reverse() method reverses the order of the elements in an array.