Shuffle Array JavaScript | Simple example code

The simplest way to shuffle an array in JavaScript is to provide a custom function to a .sort().

But the best way to shuffle an array and have a truly random distribution of items, you have to implement the Fisher-Yates algorithm.

Shuffle Array JavaScript Example

Complete HTML example codes:

Simple way

Output:

Shuffle Array JavaScript

The Fisher-Yates algorithm

Output:

shuffle an array in JavaScript

How to Shuffle an Array and Return a New One in JS

Output:

Shuffle an Array and Return a New One in JS

Do comment if you have any doubts and suggestion on this JS Array topic.

Note: All JS Examples codes are tested on the Firefox browser and the Chrome browser.
OS: Windows 10
Code: HTML 5 Version


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.