There is a built-in array method in Javascript that reverses the array i.e. reverse()
method.
1. Using reverse() method:
reverse() method: The reverse() method reverses the array in place, meaning it modifies the original array directly and does not return a new array.
Example:
const givenArr = [5, 2, 3, 7, 9, 15, 20];
const reversedArr1 = givenArr.reverse();
// Prints the reversed array
console.log(reversedArr1); // Output: [20, 15, 9, 7, 3, 2, 5]
console.log(givenArr); // Output: [20, 15, 9, 7, 3, 2, 5]
- The reverse() method directly modifies the original array by reversing its elements.
- The reversed array is also returned as the result of the method
In the above example, If you want to avoid modifying the original array, you can first create a copy of the array using .slice() and then apply .reverse() to the copy.
const givenArr = [5, 2, 3, 7, 9, 15, 20];
// Create a copy of the array and reverse the copy
const reversedArr = givenArr.slice().reverse();
console.log(reversedArr); // Output: [20, 15, 9, 7, 3, 2, 5]
console.log(givenArr); // Output: [5, 2, 3, 7, 9, 15, 20]
2. Using a for-loop:
const givenArr = [5, 12, 3, 7, 21, 9, 45, 90];
const printReversedArr = (arr) => {
const arrLen = arr.length;
for(let i = 0; i < arrLen / 2; i++) {
let temp = arr[i];
arr[i] = arr[arrLen - 1 - i];
arr[arrLen - 1 - i] = temp
}
return arr ;
}
console.log(printReversedArr(givenArr)); // Output: [90, 45, 9, 21, 7, 3, 12, 5]
3. Using a for-loop (with a new array):
const givenArr = [5, 12, 3, 7, 21, 9, 45, 90];
const getReverseArr = (arr) => {
let reversedArr = [];
for(let i = arr.length - 1; i >= 0 ; i --) {
reversedArr.push(arr[i]);
}
return reversedArr;
}
console.log(getReverseArr(givenArr)); // Output: [90, 45, 9, 21, 7, 3, 12, 5]
4. Using forEach() method:
const givenArr = [ 12, 3, 21, 9, 45, 22, 7];
const reverseArray = (arr) => {
let reversedArr = [];
arr.forEach(item => {
reversedArr.unshift(item);
});
return reversedArr;
}
console.log(reverseArray(givenArr)); // Output: [7, 22, 45, 9, 21, 3, 12]
5. Using reduce() method:
const givenArr = [ 12, 3, 21, 9, 45, 22, 7];
const reverseArray = (arr) => {
return arr.reduce((reversed, current) => [current, ...reversed], []);
}
console.log(reverseArray(givenArr)); // Output: [7, 22, 45, 9, 21, 3, 12]
6. Using unshift() method:
const givenArr = [5, 2, 3, 7, 9, 15, 20];
const reverseArray = (arr) => {
let reversedArr = [];
for (let i = 0; i < arr.length; i++) {
reversedArr.unshift(arr[i]);
}
return reversedArr;
}
console.log(reverseArray(givenArr)); // Output: [20, 15, 9, 7, 3, 2, 5]
7. Using a while loop (in-place modification):
const givenArr = [10, 6, 9, 3, 4, 5, 1];
const reverseArray = (arr) => {
let start = 0;
let end = arr.length - 1;
while (start < end) {
// Swap elements at start and end
let temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
return arr;
}
console.log(reverseArray(givenArr)); // Output: [1, 5, 4, 3, 9, 6, 10]
8. Using recursion:
const givenArr = [10, 6, 9, 3, 4, 5, 1];
const reverseArray = (arr) => {
if (arr.length === 0) {
return arr;
} else {
return reverseArray(arr.slice(1)).concat(arr[0]);
}
}
console.log(reverseArray(givenArr)); // Output: [1, 5, 4, 3, 9, 6, 10]
console.log("Original Array: ", givenArr); // Output: Original Array: [10, 6, 9, 3, 4, 5, 1]
Top comments (0)