Friday 14 September 2018

JavaScript: Reverse the elements of array

'reverse' method is used to reverse all elements of array.

Syntax
arrayName.reverse()

array.html
<!DOCTYPE html>

<html>

<head>
    <title>Array</title>
</head>

<body>
    <script type="text/javascript">
        var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];

        document.write("arr = " + arr + "<br />");

        arr.reverse();
        document.write("Reversing the elements of array arr <br />");

        document.write("arr = " + arr + "<br />");
    </script>
</body>

</html>

Open above page in browser, you can able to see following content.

arr = 1,2,3,4,5,6,7,8,9
Reversing the elements of array arr
arr = 9,8,7,6,5,4,3,2,1



Previous                                                 Next                                                 Home

No comments:

Post a Comment