# Array.prototype.rotate

**URL:** <https://es.discourse.group/t/array-prototype-rotate/1488>\
**Category:** 💡 Ideas\
**Tags:** proposal\
**Created:** [October 9, 2022, 3:03pm UTC](https://es.discourse.group/t/array-prototype-rotate/1488 "2022-10-09T15:03:38Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![retraigo](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/retraigo/32/1525_2.png) [@retraigo](https://es.discourse.group/u/retraigo)\
**Post date:** [October 9, 2022, 3:03pm UTC](https://es.discourse.group/t/array-prototype-rotate/1488/1 "2022-10-09T15:03:38Z")

</div>

A built-in method for arrays that shifts the array's elements to the left or right, without changing the length of the array.

eg.

```javascript
// shift left by two positions
let a = [1, 2, 3, 4, 5];
console.log(a.rotate(2)); // [1, 2]
console.log(a); // [3, 4, 5, 1, 2]

```

```javascript
// shift right by two positions
// same as shifting left by minus two positions
let a = [1, 2, 3, 4, 5];
console.log(a.rotate(-2)); // [5, 4]
console.log(a); // [4, 5, 1, 2, 3]

```

A very low-effort way of doing these currently:

```typescript
function rotateLeft(arr: any[], places: number): any[] {
    const res = new Array(places);
    for(let i = 0; i < places; ++i) {
        const item = arr.shift();
        arr.push(item);
        res[i] = item;
    }
    return res;
}

function rotateRight(arr: any[], places: number): any[] {
    const res = new Array(places);
    for(let i = 0; i < places; ++i) {
        const item = arr.pop();
        arr.unshift(item);
        res[i] = item;
    }
    return res;
}

```

---

<div class="post-metadata">

**Author:** ![ljharb](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/ljharb/32/8_2.png) [@ljharb](https://es.discourse.group/u/ljharb)\
**Post date:** [October 9, 2022, 3:15pm UTC](https://es.discourse.group/t/array-prototype-rotate/1488/2 "2022-10-09T15:15:07Z")

</div>

What’s the use case? Why would we want to add more mutating methods to arrays? You can do this in a non-mutating way with % and an index, or with slice and concat.

---

<div class="post-metadata">

**Author:** ![retraigo](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/retraigo/32/1525_2.png) [@retraigo](https://es.discourse.group/u/retraigo)\
**Post date:** [October 9, 2022, 3:34pm UTC](https://es.discourse.group/t/array-prototype-rotate/1488/3 "2022-10-09T15:34:42Z")

</div>

The use case is indeed niche.

The `shift()` method can be used in a queue. The `rotate()` method is just the same but for a circular queue.

It can also be used for cases like spinning a wheel of fortune or a revolver. In these cases, you want to rotate it `n` times from the starting point. The next rotation should start from the previous end point.

The method can be made to not mutate the target array using a `slice()`, like how a reversed version of an array can be obtained with `target.slice().reverse()`.

---

<div class="post-metadata">

**Author:** ![ljharb](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/ljharb/32/8_2.png) [@ljharb](https://es.discourse.group/u/ljharb)\
**Post date:** [October 9, 2022, 5:11pm UTC](https://es.discourse.group/t/array-prototype-rotate/1488/4 "2022-10-09T17:11:33Z")

</div>

If the use case is niche, and it's not difficult to do in userland, that seems like a pretty strong argument against adding it to the language?

---

<div class="post-metadata">

**Author:** ![Rudxain](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/rudxain/32/2272_2.png) [@Rudxain](https://es.discourse.group/u/Rudxain)\
**Post date:** [October 11, 2022, 4:16pm UTC](https://es.discourse.group/t/array-prototype-rotate/1488/5 "2022-10-11T16:16:39Z")

</div>

> [@retraigo](#):
>
> The `rotate()` method is just the same but for a circular queue.

For me, this is probably the best "defense" for these methods. But it can be "debunked" by an actual `CircularBuffer` data structure, which is specially optimized for that task. But JS doesn't have such `class`. The "circular queue argument" is in favor of rotational queues, not necessarily the methods proposed.

I agree with ljharb, it doesn't seem to be worth adding to the spec. We need more research to back it up, if we ever hope it'll get added
