JavaScript Array reverse()

JavascriptWeb DevelopmentFront End TechnologyObject Oriented Programming

The reverse() method of JavaScript is used to reverse the array elements.

The syntax is as follows −

array.reverse()

Let us now implement the reverse() method in JavaScript −

Example

 Live Demo

<!DOCTYPE html>
<html>
<body>
   <h2>Demo Heading</h2>
   <p id="test"></p>
   <script>
      var arrStud = ["Laptop", "Desktop", "Tablet"];
      document.getElementById("test").innerHTML = "Initial array = "+arrStud+", <br>Reversed array =       "+arrStud.reverse();
   </script>
</body>

Output

Example

 Live Demo

<!DOCTYPE html>
<html>
<body>
   <h2>Car Variant</h2>
   <button onclick="display()">Reverse</button>
   <p id="test"></p>
   <script>
      var car = ["Hatchback", "Convertible", "SUV", "AUV", "MUV"];
      document.getElementById("test").innerHTML = "Initial Array = "+car;
      function display() {
         car.reverse();
         document.getElementById("test").innerHTML = "Reversed Array = "+car;
      }
   </script>
</body>

Output

Click on “Reverse” button above −

raja
Published on 17-Dec-2019 12:13:13
Advertisements