JavaScript array reverse example

JavaScript Array class have one method reverse()
which will reverse the order of element into the array. This has a simple syntax
as follows:
arrayname.reverse()
In this example we have created an array "arr"
which contains three elements. Now we can reverse the array elements order
by using the method reverse(). Here is the full example code of
JavaScript array reverse() method as follows:
<html>
<head>
<title>JavaScript array reverse() example</title>
<script type="text/javascript">
var arr = new Array(3);
arr[0]="1";
arr[1]="2";
arr[2]="3";
document.write("<b>Array 'arr' is </b>=>"+arr+"<br> ");
document.write("<b>Reversed array is</b> =>"+arr.reverse()+"<br> ");
</script>
</head>
<body>
<h2>
Example of reversing an array
</h2>
</body>
</html> |
Output:

Download Source Code

|