Home >>JavaScript Array Reference >JavaScript Array reverse() Method
JavaScript reverse() method is used to reverse the order of the elements in the given input array. It changes the original given array. It makes the arrays last element first and the first element the last.
Syntax:array.reverse()
Method | Chrome | Edge | Firefox | Safari | Opera |
---|---|---|---|---|---|
reverse() | 1.0 | 5.5 | 1.0 | Yes | Yes |
<html> <body> <p>Click the button to see the Output.</p> <button onclick="myFunction()">Try it</button> <p id="demo"></p> <script> var x = ["PHP", "Python", "Java", "JavaScript"]; document.getElementById("demo").innerHTML = x; function myFunction() { x.reverse(); document.getElementById("demo").innerHTML = x; } </script> </body> </html>
Click the button to see the Output.
<html> <body> <p>Click the button to see the Output.</p> <button onclick="myFunction1()">Try it</button> <p id="demo1"></p> <script> var y = ["1", "2", "3", "4", "5", "6", "7", "8", "9"]; document.getElementById("demo1").innerHTML = y; function myFunction1() { y.reverse(); document.getElementById("demo1").innerHTML = y; } </script> </body> </html>
Click the button to see the Output.