Array.reverse() (Method)

Reverse the order of array elements.

Availability:

ECMAScript edition - 2
JavaScript - 1.1
JScript - 3.0
Internet Explorer - 4.0
Netscape - 3.0
Netscape Enterprise Server - 2.0
Opera - 3.0
Property/method value type:Array object
JavaScript syntax:-myArray.reverse()

The elements in the array are rearranged into reverse order. The Array object is returned as the result.

Note that the reverse() method may possibly be applied to other object types. Host objects may support the reverse() method, but it will be in an implementation dependant manner.

The result of this method is the array with its elements in reversed order.

Example code:

   <HTML>

   <HEAD>

   </HEAD>

   <BODY>

   <SCRIPT>

   // Demonstrate array joins

   myString1 = "This is a sentence made of words.";

   document.write("Original input string<BR>")

   document.write(myString1)

   myArray = myString1.split(" ");

   document.write("<BR><BR>String split into an array<BR>")

   displayArrayAsTable(myArray);

   myArray.reverse();

   document.write("<BR><BR>Array reversed<BR>")

   displayArrayAsTable(myArray);

   myString2 = myArray.join(" ");

   document.write("<BR><BR>Array joined up as a string<BR>")

   document.write(myString2)

   

   // Display an array in a table

   function displayArrayAsTable(anArray)

   {

      myLength = anArray.length;

      document.write("<TABLE BORDER=1>");

      for(myIndex = 0; myIndex < myLength; myIndex++)

      {

         document.write("<TR><TD>");

         document.write(myIndex);

         document.write("</TD><TD>");

         document.write(anArray[myIndex]);

         document.write("</TD></TR>");

      }

      document.write("</TABLE>")

   }

   </SCRIPT>

   </BODY>

   </HTML>

See also:Array.prototype

insert figure 0013

Cross-references:

ECMA 262 edition 2 - section - 15.4.4.4

ECMA 262 edition 3 - section - 15.4.4.8