> For the complete documentation index, see [llms.txt](https://lochiwei.gitbook.io/web/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://lochiwei.gitbook.io/web/js/val/builtin/arr/ext/arr.shuffle.md).

# 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](/web/appendix/custom/helper-functions.md)
* [string.shuffle()](/web/js/val/prim/str/method/string.shuffle.md)
  {% 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 %}
