JavaScript reverse() | Reverse an Array

The JavaScript reverse() method is used to reverse an array. For example:

HTML with JavaScript Code
<!DOCTYPE html>
<html>
<body>

   <p id="xyz"></p>

   <script>
      const cities = ["Tokyo", "Bangkok", "Dubai", "Berlin", "London"];
      cities.reverse();
      document.getElementById("xyz").innerHTML = cities;
   </script>
   
</body>
</html>
Output

JavaScript reverse() Syntax

The syntax of reverse() method in JavaScript is:

array.reverse()

JavaScript reverse() Example

Here is an example of reverse() method that shows how we can use this method to reverse any array:

HTML with JavaScript Code
<!DOCTYPE html>
<html>
<body>

   <p>Original Array:-<br><span id="original"></span></p>
   <p>Array after Reverse:-<br><span id="reverse"></span></p>
   
   <script>
      const x = ["Tokyo", "Bangkok", "Dubai", "Berlin", "London"];
      document.getElementById("original").innerHTML = x;

      x.reverse();
      document.getElementById("reverse").innerHTML = x;
   </script>
   
</body>
</html>
Output

Original Array:-

Array after Reverse:-

JavaScript Online Test


« Previous Tutorial Next Tutorial »



Like/Share Us on Facebook 😋