How to use Array unshift() and pop() Methods In Javascript

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 using the above approach.

Javascript




let Arr = ['GFG_1', 'GFG_2', 'GFG_3', 'GFG_4'];
 
function arrayRotate(arr) {
    arr.unshift(arr.pop());
    return arr;
}
 
function myGFG() {
    let rotateArr = arrayRotate(Arr);
    console.log("Elements of array = ["
        + rotateArr + "]");
}
 
myGFG();


Output

Elements of array = [GFG_4,GFG_1,GFG_2,GFG_3]

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: 

Similar Reads

Method 1: Using Array unshift() and pop() Methods

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....

Method 2: Using Array push() and shift() Methods

...