Javascript Array - reverse method
The reverse() method takes the array and reverses the order of the elements so the first item becomes the last and the last item becomes the first.
Take a look at the following example
<html> <body> <script type="text/javascript"> var x = [5,7,9,11]; x.reverse(); document.writeln(x); </script> </body> </html> |
Try this example online here .
In conjunction with the sort method, the reverse method can be used to sort the elements in descending order.
<html> <body> <script type="text/javascript"> var cars = ["Toyota", "Honda", "Audi", "Hyundai"]; cars.sort(); cars.reverse(); document.writeln('Car brands :' + cars + '<br />'); </script> </body> </html> |
Try this example online here .