Table of Contents
- Using the reverse method
- Using a decrementing For Loop
- Using the Unshift() Method
- Without using a new array or the reverse() method
- Other Related Concepts
Using the reverse methodAs the name suggests, this method reverses the order of array elements by modifying the existing array.
Using a decrementing For Loop
In the above example, we use a decrementing loop to traverse the array arr from backward and store each element to the new array arr1. This method does not modify the original array.
Using the Unshift() Method
This method is very similar to the previous method and does not modify the original array. Instead of using a for loop, this method uses forEach() & unshift() methods. forEach() method performs an operation for each element of an array and the unshift method adds a new element to the beginning of an array.
Without using a new array or the reverse() methodIn the previous two methods, we did not modify the existing array and stored the reversed elements in a new array. Now, we shall look at how one can reverse and modify an existing array without using the reverse method.
In this method, we traverse only one half of the length of the given array and swap the elements in equal distance from first and the last i,e, first element and last and second with second last and so on.