There are many ways to iterate over a set in reverse order in JavaScript. However, in this article, I will show you the way I usually use when encountering this requirement with some methods like Array.from() combined with array .push() or Array.from() in combination with the reverse() method.
Iterate over a set in reverse order in JavaScript
We see the appearance of the Array.from() method, so to learn the above two ways, we will first learn the Array.from() method first.
Syntax:
Array.from(arrayLike)
Parameter:
- arrayLike: Is an array-like object or iterable to convert to an array.
Example:
var set = new Set([1, 3, 4, 2]); var arr = Array.from(set);
This method will return a new array containing all the values of arrayLike. In this tutorial, we can use the Array.from() method to convert the given set into an array. From there, we can use this array to iterate over a set in reverse order in Javascript.
Use the Array.from() method in conjunction with push()
After getting the array from Array.from(), I will use the For iteration to iterate the above array in order from right to left (reverse), then save each of its elements with the push() method into a new array reverse to the array generated from the Array.from() method.
push() method
Syntax:
array.push(item1, item2, …, itemX)
Parameter:
- item1, item2, …, itemX: The element(s) that we want to add to the array
After learning the push() method, I will use it and Array.from() to iterate over a set in reverse order as follows:
var set = new Set([1, 3, 4, 2]); var result = []; var arr = Array.from(set); // Iterate over an inverted set using Array.from() and push() methods for (let index = arr.length - 1; index >= 0; index--) { result.push(arr[index]); } console.log(result);
Output:
(4) [2, 4, 3, 1]
In the above example, I created a new array that stores the reversed values using the For loop. Then output the array with the values reversed from the order of the given set object.
Use Array.from() method in combination with reverse() method
Different from the above when we use the For loop and push() method to create a new array that stores the reversed values from the array containing the elements of the given set created from Array.from(). In this way, I will use a built-in method to do this, the reverse() method.
Syntax:
array.reverse()
The reverse() method returns a new array that has been reversed in the order of the original array. I will use it to iterate over a set in reverse order as follows:
var set = new Set([1, 3, 4, 2]); var arr = Array.from(set); // Iterate over an inverted set using Array.from() and reverse() methods var result = arr.reverse(); console.log(result);
Output:
(4) [2, 4, 3, 1]
This way, we can create a new array to iterate over an inverted set more easily by the reverse() method.
Summary
With the two ways I have given you in this article, iterate over a set in reverse order in JavaScript is not difficult. The fastest way is using the Array.from() method in combination with the reverse() method. I hope this article helps you and your program. Good luck!
Maybe you are interested:
- Iterate over the Elements in a Set using JavaScript
- How to Get the First Element of a Set in JavaScript
- Get the Difference between Two Sets using JavaScript

My name is Tom Joseph, and I work as a software engineer. I enjoy programming and passing on my experience. C, C++, JAVA, and Python are my strong programming languages that I can share with everyone. In addition, I have also developed projects using Javascript, html, css.
Job: Developer
Name of the university: UTC
Programming Languages: C, C++, Javascript, JAVA, python, html, css