How to rotate array elements by using JavaScript ? Last Updated : 30 Jun, 2023 Comments Improve Suggest changes Like Article Like Report 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: Using Array unshift() and pop() MethodsUsing Array push() and shift() MethodsMethod 1: Using Array unshift() and pop() MethodsWe 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(); OutputElements of array = [GFG_4,GFG_1,GFG_2,GFG_3] Method 2: Using Array push() and shift() MethodsWe 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 using the above approach JavaScript let Arr = ['GFG_1', 'GFG_2', 'GFG_3', 'GFG_4']; function arrayRotate(arr) { arr.push(arr.shift()); return arr; } function myGFG() { let rotateArr = arrayRotate(Arr); console.log("Elements of array = [" + rotateArr + "]"); } myGFG(); OutputElements of array = [GFG_2,GFG_3,GFG_4,GFG_1] Comment More infoAdvertise with us Next Article How to rotate array elements by using JavaScript ? P PranchalKatiyar Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Questions Similar Reads How to rotate an HTML div element 90 degrees using JavaScript ? An element can be rotated 90 degrees by using the transform property. This property is used to move, rotate, scale, and others to perform various kinds of transformation to elements. ApproachThe rotate() transformation function can be used as the value to rotate the element. It takes one parameter 2 min read How to rotate an element using CSS ? The purpose of this article is to rotate an HTML element by using CSS transform property. This property is used to move, rotate, scale and to perform various kinds of transformation of elements. The rotate() function can be used to rotate any HTML element or used for transformations. It takes one pa 1 min read Swapping two array elements in a single line using JavaScript In JavaScript, there exist many ways by which one can swap two array elements. In this article, we will discuss a way in which one can swap two array elements in JavaScript in a single line. The input and output would be as follows. Input: arr = { 10, 20, 40, 30 }Output: arr = { 10, 20, 30, 40 } // 3 min read Rotate an Array to the Right by K Steps using JavaScript One can Rotate an array to the left by k steps using JavaScript. We are given an input array and a value k we have to rotate that array by k steps in the right direction.Example:Input arr = [1 , 2 , 3 , 4 , 5 ]k = 3 Output arr = [ 3 , 4 , 5 , 1 , 2]Below are different approaches to Rotate an array t 4 min read How to make a Round Slider using JavaScript ? Round Slider is the UI element that allows us to select a value by dragging a handle along the circumference of the circular track. The below approaches can be used to accomplish this task. Table of Content Using CanvasUsing jQueryUsing CanvasIn this example, we're creating a round slider using the 4 min read Javascript Program to Rotate Matrix Elements Given a matrix, clockwise rotate elements in it.Examples:Input: 1 2 3 4 5 6 7 8 9 Output: 4 1 2 7 5 3 8 9 6 For 4*4 matrix Input: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Output: 5 1 2 3 9 10 6 4 13 11 7 8 14 15 16 12The idea is to use loops similar to the program for printing a matrix in spiral form. 5 min read How to move an array element from one array position to another in JavaScript? In JavaScript, we can access an array element as in other programming languages like C, C++, Java, etc. Also, there is a method called splice() in JavaScript, by which an array can be removed or replaced by another element for an index. So to move an array element from one array position to another 5 min read Javascript Program for Search an element in a sorted and rotated array An element in a sorted array can be found in O(log n) time via binary search. But suppose we rotate an ascending order sorted array at some pivot unknown to you beforehand. So for instance, 1 2 3 4 5 might become 3 4 5 1 2. Devise a way to find an element in the rotated array in O(log n) time. Exam 7 min read How to Move a Key in an Array of Objects using JavaScript? The JavaScript array of objects is a type of array that contains JavaScript objects as its elements.You can move or add a key to these types of arrays using the below methods in JavaScript:Table of ContentUsing Object Destructuring and Map()Using forEach() methodUsing for...of LoopUsing reduce() met 5 min read How to Find Max Sum (i*arr[i]) with Array Rotations in JavaScript? Given an array where rotation operations are allowed, the task is to rotate the array any number of times and find the maximum possible sum of i*arr[i]. Examples : Input: arr = [1, 20, 2, 10] Output: 72 Explanation: We can get 72 by rotating array twice. [2, 10, 1, 20] 20*3 + 1*2 + 10*1 + 2*0 = 72 I 4 min read Like