/

HTML / CSS / JavaScript Tutorial

JavaScript Array method: reverse()

[this page | pdf | back links]

The reverse() method (when applied to a JavaScript array) removes the last element of the array and returns that element.

 

It has the following syntax with no parameters. It returns an array as above.

 

array.reverse()

 

EXAMPLE:


HTML USED IN THIS EXAMPLE:
<!DOCTYPE html>
<html> <!-- Copyright (c) Nematrian Limited 2018 -->
<head>
<style>
table,th,tr,td {border: 1px solid black; border-collapse: collapse;}
</style>
</head>
<body>
<table>
<tr>
<th>Example</th>
<th>Resulting value of <code>x</code></th>
</tr>
<tr>
<td><code id="Example"></code></td>
<td><code id="Result"></code></td>
</tr>
</table>

<script>
var h = ["a", "b", "c"];
document.getElementById("Example").innerHTML =
  'var h = ["a", "b", "c"];<br>' +
  'var x = h.reverse();';
document.getElementById("Result").innerHTML
  = h.reverse();
</script>

</body>
</html>

FUNCTION THAT MAY ASSIST IN TESTING WHETHER FEATURE IS SUPPORTED:
function isSupportedJavaScriptMethodArrayReverse() {
  var h = [1, 2, 3]; return !!h.reverse;
}


NAVIGATION LINKS
Contents | Prev | Next | JavaScript Arrays


Desktop view | Switch to Mobile