CancelImage Upload

How to flip an array and place its values in reverse order in JavaScript

Sparse as JavaScript array functions may be, there is one available to reverse the order of the array. It's name, no surprise, is reverse(), and here is how it is defined:

var fruits = new Array('Oranges', 'Bananas', 'Strawberries', 'Apples');

fruits.reverse();

The above code example is all that is needed to reverse the order. You will not need to make any equals statements, such as:

var fruits = new Array('Oranges', 'Bananas', 'Strawberries', 'Apples');

fruits = fruits.reverse();

Here is a more extensive example of the reversed array being loaded into a span tag via getElementById():

<html>
<head>
<title>How to flip an array in JavaScript</title>
<script type="text/javascript">

function arrayReverse()
{

        var fruits = new Array('Oranges', 'Bananas', 'Strawberries', 'Apples');

        fruits.reverse();

        document.getElementById("display").innerHTML = fruits;

}

</script>
</head>
<body onload="javascript:arrayReverse();">

<span id="display"></span>

</body>
</html>


Login
Want to leave a comment?

No problem. Just enter your email and password below.


register | home | reminder

myDesignTool Networking • www.mydesigntool.cominfo@mydesigntool.com