How to shuffle an array in JavaScript

In vanilla JavaScript, there is no direct way to randomize array elements. Although languages like PHP and Ruby provide built-in methods for shuffling arrays, JavaScript does not.

The most commonly used solution to randomize an array is the Fisher–Yates shuffle algorithm:

  1. Write down the numbers from 1 through N.
  2. Pick a random number k between one and the number of unstruck numbers remaining (inclusive).
  3. Counting from the low end, strike out the kth number not yet struck out, and write it down at the end of a separate list.
  4. Repeat from step 2 until all the numbers have been struck out.
  5. The sequence of numbers written down in step 3 is now a random permutation of the original numbers.

Here is a JavaScript function that implements the Fisher-Yates shuffle algorithm:

const shuffle = (array) => {

    // loop all elements
    for (let i = array.length - 1; i > 0; i--) {

        // pickup a random element
        const j = Math.floor(Math.random() * i);
        const temp = array[i];

        // swap it with the current element
        array[i] = array[j];
        array[j] = temp;

Now you can call the above function by passing in the array as an argument:

const fruits = ['πŸ‘', 'πŸ“', 'πŸ‰', 'πŸ‡', 'πŸ’'];

// shuffle array


// [ 'πŸ“', 'πŸ‰', 'πŸ’', 'πŸ‘', 'πŸ‡' ]

The above solution shuffles the original array and does not return a copy. If you want to keep the original array unchanged, use the Array.slice() method to pass in a copy:

const fruits2 = fruits.slice();


If you are already using Lodash in your project, just use the _.shuffle() method to randomize an array. This method implements the same Fisher-Yates shuffle algorithm to shuffle array elements, and returns a new array:

const _ = require('lodash');

const fruits = ['πŸ‘', 'πŸ“', 'πŸ‰', 'πŸ‡', 'πŸ’'];

// shuffle array
const shuffled = _.shuffle(fruits);


// [ 'πŸ‰', 'πŸ’', 'πŸ“', 'πŸ‡', 'πŸ‘' ]

Take a look this guide to learn more about JavaScript arrays and how to use them to store multiple values in a single variable.

✌️ Like this article? Follow me on Twitter and LinkedIn. You can also subscribe to RSS Feed.

You might also like...

Digital Ocean

The simplest cloud platform for developers & teams. Start with a $200 free credit.

Buy me a coffee β˜•

If you enjoy reading my articles and want to help me out paying bills, please consider buying me a coffee ($5) or two ($10). I will be highly grateful to you ✌️

Enter the number of coffees below:

✨ Learn to build modern web applications using JavaScript and Spring Boot

I started this blog as a place to share everything I have learned in the last decade. I write about modern JavaScript, Node.js, Spring Boot, core Java, RESTful APIs, and all things web development.

The newsletter is sent every week and includes early access to clear, concise, and easy-to-follow tutorials, and other stuff I think you'd enjoy! No spam ever, unsubscribe at any time.