# 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 %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://lochiwei.gitbook.io/web/js/val/builtin/arr/ext/arr.shuffle.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
