➤ How to Code a Game
➤ Array Programs in Java
➤ Java Inline Thread Creation
➤ Java Custom Exception
➤ Hibernate vs JDBC
➤ Object Relational Mapping
➤ Check Oracle DB Size
➤ Check Oracle DB Version
➤ Generation of Computers
➤ XML Pros & Cons
➤ Git Analytics & Its Uses
➤ Top Skills for Cloud Professional
➤ How to Hire Best Candidates
➤ Scrum Master Roles & Work
➤ CyberSecurity in Python
➤ Protect from Cyber-Attack
➤ Solve App Development Challenges
➤ Top Chrome Extensions for Twitch Users
➤ Mistakes That Can Ruin Your Test Metric Program
Reverse An Array In JavaScript | This post will look at several JavaScript methods that can reverse an array. After declaring the array in JavaScript, we will reverse it using various techniques. Also see:- JavaScript Array slice() Method
JavaScript Array reverse() Method
This solution uses the reverse() form found under arrays in JavaScript, making it the simplest and most natural method. We will first declare an array with a set of values and then use the reverse() method to output the array’s values backward.
let numbers_array = [1, 2, 3, 4, 5];
console.log("Original Array: ");
console.log(numbers_array);
numbers_array.reverse();
console.log("Reversed Array: ");
console.log(numbers_array);
Output:-
Original Array:
[ 1, 2, 3, 4, 5 ]
Reversed Array:
[ 5, 4, 3, 2, 1 ]
Reverse An Array In JavaScript Using loop
We can reverse an array using the for loop. To print the array’s items in reverse order, we create an array and then use it for a loop.
let original_array = [10, 2, 30, 4, 50];
let reversed_array = [];
console.log("The Original Array: ");
console.log(original_array);
for (let i = original_array.length - 1; i >= 0; i--) {
reversed_array.push(original_array[i]);
}
console.log("The Reversed Array: ");
console.log(reversed_array);
Output:-
The Original Array:
[ 10, 2, 30, 4, 50 ]
The Reversed Array:
[ 50, 4, 30, 2, 10 ]
Reverse an Array In JavaScript Using unshift() Method
This strategy uses the unshift() JavaScript technique. Using this approach, the array’s first members are added. The forEach() loop will execute operations on each array element. We will utilize a freshly generated array to add the elements from the preceding array but in reverse order.
let original_array = [1, 2, 3];
let reversed_array = [];
console.log("The Original Array: ");
console.log(original_array);
original_array.forEach((element) => {
reversed_array.unshift(element);
});
console.log("The Reversed Array: ");
console.log(reversed_array);
Output:-
The Original Array:
[ 1, 2, 3 ]
The Reversed Array:
[ 3, 2, 1 ]
Reverse an Array In JavaScript Using reduce()
With this method, each element has a callback function applied, and the results are compiled into an accumulator. First, we will add each piece to the beginning of the array using the reduce method on an array with an empty serving as the accumulator. We finally receive the original array’s inverse.
let original_array = [1, 2, 3, 4];
let reversed_array = [];
console.log("Original Array: ");
console.log(original_array);
reversed_array = original_array.reduce((acc, item) => [item].concat(acc), []);
console.log("Reversed Array: ");
console.log(reversed_array);
Output:-
Original Array:
[ 1, 2, 3, 4 ]
Reversed Array:
[ 4, 3, 2, 1 ]
If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!