# arr.shuffle()

{% tabs %}
{% tab title="shuffle()" %}
```javascript
/**
 * ⭐️ shuffle(arr)
 * - arr is shuffled in place
 * @param arr array to shuffle
 */
export function shuffle(arr) {

    var n = arr.length;

    for (let i = n - 1; i > 0; i--) {
        let j = randomInt(0, i);
        [arr[i], arr[j]] = [arr[j], arr[i]];
    }

    return arr;
}

// ⭐️ array.shuffle()
Array.prototype.shuffle = function () {
    return shuffle(this);
};

```
{% endtab %}

{% tab title="⬇️ 應用" %}
* [helper functions](../../../../../appendix/custom/helper-functions)
* [string.shuffle()](../../../prim/str/method/string.shuffle)
{% endtab %}

{% tab title="📗 參考" %}
* [x] DEV ⟩ [How to shuffle an array in JavaScript](https://dev.to/codebubb/how-to-shuffle-an-array-in-javascript-2ikj)
{% endtab %}
{% endtabs %}
