DEV Community

Sergei
Sergei

Posted on

JavaScript ES6 one-liners: rotate an array

const arr = [1,2,3,4,5];
const shift = 2;

// rotate left by n < array length:
console.log(`rotated: ${[...arr.slice(shift), ...arr.slice(0,shift)]}`);
// rotated: 3,4,5,1,2

Discussion (0)