DEV Community

Discussion on: Two Ways to Rotate an Array in JavaScript

Collapse
jonrandy profile image
Jon Randy • Edited on

Why not just this?

const rotateArray = (arr, k) => arr.concat(arr).slice(k, k+arr.length);
Enter fullscreen mode Exit fullscreen mode
Collapse
alekseiberezkin profile image
Aleksei Berezkin

Or, to avoid arr concatenation if it can be large,

const rotateArray = (arr, k) => arr.slice(k).concat(arr.slice(0, k));
Enter fullscreen mode Exit fullscreen mode