Array reverse()

JavaScript Array reverse() Definition

The reverse () method reverses the array. The first element of the array becomes the last element and vice versa.

Syntax:

array.reverse()


JavaScript Array reverse() Examples

Example 1

        let list = ["a", "b", "c", "d"];
        console.log("Normal Array: " + list);

        let newList = list.reverse();
        console.log("Reverse Array: "+ newList)

Output:

Normal Array: a,b,c,d
Reverse Array: d,c,b,a


You can define the reverse () function yourself

        function reverseArr(input) {
            var ret = new Array;
            for (var i = input.length - 1; i >= 0; i--) {
                ret.push(input[i]);
            }
            return ret;
        }

Example:

        var x = ["a","b","c","d"]
        var y= reverseArr(a);
        console.log(x)
        console.log(y)

[“a”,”b”,”c”,”d”]
[“d”,”c”,”b”,”a”]


Browser Support

Chrome1
Edgeyes
Firefox1
Internet Explorer5.5
Operayes
Safariyes
Android webviewyes
Chrome for Androidyes
Edge mobileyes
Firefox for Android4
Opera Androidyes

2 thoughts on “Array reverse()

Add yours

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Create a website or blog at WordPress.com

Up ↑

Design a site like this with WordPress.com
Get started