DEV Community 👩‍💻👨‍💻

Cover image for Array Rotation in JS

Posted on

Array Rotation in JS

Hello Guys today i will show you how to rotate left 1-d array in JS
The rotation will depent on the input like 2 means rotation.
A left rotation operation on an array of size n shifts each of the array's elements 1 unit to the left.

Let's get started...

function rotateLeft(d, arr) {
    // Write your code here
    let newArray = []
    if(d > arr.length){
      return "Rotation value cannot be bigger than array length"
      for(let i=0;i<d;i++){

      let rotated = arr.concat(newArray)
      return rotated


let array = [1,2,3,4,5,6]
let rotation = 2
Enter fullscreen mode Exit fullscreen mode

Output -

[ 3, 4, 5, 6, 1, 2 ]
Enter fullscreen mode Exit fullscreen mode


  • Inside the function , we have created an empty array which will hold the elements after rotation.
  • After that we provide a condition where we will check if the number of rotation is greater than the array length or not.
  • In the else part , we used the for loop up to the number of rotations (exclusive, means if the rotation value is 4 then the for loop will be executed from 0-3)
  • Inside the loop we removed the element from the starting up to the rotation value - 1(because array index starts from 0) and then pushed those removed elements in the empty array we have created.
  • In the end we have combined the original array with array containing the removed elements which will come at the end.

If you have a better approach or solution to this code , please mention it in the comment section.

^^You can help me by some donation at the link below Thank you👇👇 ^^
☕ --> <--

Also check these posts as well

Top comments (6)

vkoonline profile image
Medet Tleukabiluly ✎

return arr.slice(d).concat(arr.slice(0, d)) is enough

joelbonetr profile image

Also Number.parseInt(arr.length % d) could be useful in case the number is greater than the arr length.

const completeRotations = arr.length / d;

let extraSteps = Number.parseInt(arr.length % d);
Enter fullscreen mode Exit fullscreen mode

With that you can simply do a single complete rotation and keeo going the extra steps instead of failing 😁

shubhamtiwari909 profile image
Mysterio Author

Thank you i will try this as well 😉

shubhamtiwari909 profile image
Mysterio Author • Edited on

Thank you i will try this
Can you please explain this code?
I am getting a bit confused with it

peerreynders profile image
peerreynders • Edited on
function rotateLeft(d, arr) {
  const endToBegin = arr.slice(d);
  const beginToEnd = arr.slice(0, d);
  return endToBegin.concat(beginToEnd);

const array = [1, 2, 3, 4, 5, 6];
const rotation = 2;
console.log(rotateLeft(rotation, array)); // [3, 4, 5, 6, 1, 2]
Enter fullscreen mode Exit fullscreen mode


Array.prototype.slice() - JavaScript | MDN

The slice() method returns a shallow copy of a portion of an array into a new array object selected from start to end (end not included) where start and end represent the index of items in that array. The original array will not be modified.

Thread Thread
shubhamtiwari909 profile image
Mysterio Author

Got it thank you for explaining 😉

🌚 Friends don't let friends browse without dark mode.

Sorry, it's true.