Skip to content
Advertisements

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
Advertisements

2 Comments »

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

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

%d bloggers like this: