JavaScript Reverse an Array
To reverse an array in JavaScript, call reverse() method on this array. reverse() method reverses the order of elements in the original array, in-place.
Example
In the following example, we have taken an array in inputArr
. We reverse this array using reverse() method and store the resulting array in reversedArr
.
index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> </head> <body> <pre id="output"></pre> <script> var inputArr = ['banana', 'mango', 'apple']; displayOutput = 'Input Array : ' + inputArr; var reversedArr = inputArr.reverse(); displayOutput += '\nReversed Array : ' + reversedArr; document.getElementById("output").innerHTML = displayOutput; </script> </body> </html>
Conclusion
In this JavaScript Tutorial, we learned how to reverse an array using reverse() method.