JavaScript array reverse() method
The array.reverse() method in JavaScript is used to reverse an array in place. The reverse() method reverses the array immediately; this means that the original elements of the array are swapped, and the original sequence is lost. You don't need to make a new variable to store a pointer to these arrays in memory since the original pointers already refer us to the reversed arrays.
Where:
- array: the array to be reversed
How to reverse array using for loop?
To reverse an array, you can also use a for loop to go through the elements of the array and create a new array in reverse order. The last element of the array will be the starting point of the loop, and it will be executed until it reaches the beginning of the array. Unlike the array.reverse() method, this solution does not change the original array. A new array is created containing the elements in reverse order. Note that this method is slower and takes up extra memory space.