Array Methods to Change Element Ordering
Explore how to manipulate array element order in JavaScript using methods such as reverse and sort. Learn to control sorting with compare functions and find element positions with indexOf and lastIndexOf. This lesson helps you understand core array operations to manage data order efficiently.
Two such methods to change element ordering are discussed below:
The reverse() method #
You can change the order of elements in an array with the reverse() and sort() methods. Use reverse() to reverse the order of the elements. Invoking this method, the first item becomes the last, and the last item becomes the first and so forth for the rest of the items as the example below shows: