How to rotate array elements by using JavaScript ?

Given an array containing some array elements and the task is to perform the rotation of the array with the help of JavaScript. There are two approaches that are discussed below:

Approach 1: We can use the Array unshift() method and Array pop() method to first pop the last element of the array and then insert it at the beginning of the array.

  • Example: This example rotates the array elements.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <!DOCTYPE HTML>
    <html>
      
    <head>
        <title>
            Rotate the elements in an array
            by using JavaScript Methods
        </title>
    </head>
      
    <body style="text-align:center;">
      
        <h1 style="color:green;">
            GeeksForGeeks
        </h1>
      
        <p id="GFG_UP"></p>
      
        <button onclick="myGFG()">
            Click Here
        </button>
          
        <p id="GFG_DOWN"></p>
      
        <script>
            var array = ['GFG_1', 'GFG_2', 'GFG_3', 'GFG_4'];
            var up = document.getElementById("GFG_UP");
      
            up.innerHTML = "Click on the button to perform"
                         + " the operation<br>Array - ["
                        + array + "]";
      
            var down = document.getElementById("GFG_DOWN");
      
            function arrayRotate(arr) {
                arr.unshift(arr.pop());
                return arr;
            }
              
            function myGFG() {
                array = arrayRotate(array);
                down.innerHTML = "elements of array = ["
                                + array + "]";
            }
        </script>
    </body>
      
    </html>

    chevron_right

    
    

  • Output:

Approach 2: We can use the Array push() method and Array shift() method to shift the first element and then insert it at the end.

  • Example: This example rotates the array elements.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <!DOCTYPE HTML>
    <html>
      
    <head>
        <title>
            Rotate the elements in an array
            by using JavaScript Methods
        </title>
    </head>
      
    <body style="text-align:center;">
      
        <h1 style="color:green;">
            GeeksForGeeks
        </h1>
          
        <p id="GFG_UP"></p>
      
        <button onclick="myGFG()">
            Click Here
        </button>
          
        <p id="GFG_DOWN"></p>
      
        <script>
            var array = ['GFG_1', 'GFG_2', 'GFG_3', 'GFG_4'];
            var up = document.getElementById("GFG_UP");
            up.innerHTML = "Click on the button to perform"
                         + " the operation<br>Array - ["
                        + array + "]";
      
            var down = document.getElementById("GFG_DOWN");
      
            function arrayRotate(arr) {
                arr.push(arr.shift());
                return arr;
            }
              
            function myGFG() {
                array = arrayRotate(array);
                down.innerHTML = "elements of array = ["
                               + array + "]";
            }
        </script>
    </body>
      
    </html>

    chevron_right

    
    

  • Output:

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.