Skip to content

How to shuffle elements in a JavaScript array

Short answer:

let list = [1, 2, 3, 4, 5, 6, 7, 8, 9]
list = list.sort(() => Math.random() - 0.5)

Long answer:

I had the need to shuffle the elements in a JavaScript array.

In other words, I wanted to remix the array elements, to have them in a different order than the previous one.

Starting from an array like this:

[1, 2, 3, 4, 5, 6, 7, 8, 9]

I wanted something different any time I ran the operation, like this:

[4, 8, 2, 9, 1, 3, 6, 5, 7]
[5, 1, 2, 3, 7, 4, 9, 6, 8]
[3, 1, 4, 7, 8, 6, 2, 9, 5]

Here is the process I came up with. Given the array list:

const list = [1, 2, 3, 4, 5, 6, 7, 8, 9]

We can call the sort() method, which accepts a function that returns a value between -0.5 and 0.5:

list.sort(() => Math.random() - 0.5)

This function is ran for every element in the array. You can pass 2 elements of the array, like this: list.sort((a, b) => Math.random() - 0.5) but in this case we’re not using them. If the result of this operation is < 0, the element a is put to an index lower than b, and the opposite if the result is > 0.

You can read all the details on Array.sort() here.

Calling sort() on a list does not change the original array value.

Now you can assign the result of this operation to a new variable, like this:

const shuffled = list.sort(() => Math.random() - 0.5)

or you can also overwrite the existing list, if you declare that as a let variable:

let list = [1, 2, 3, 4, 5, 6, 7, 8, 9]
list = list.sort(() => Math.random() - 0.5)

→ Get my JavaScript Beginner's Handbook

download all my books for free

  • javascript handbook
  • typescript handbook
  • css handbook
  • node.js handbook
  • astro handbook
  • html handbook
  • next.js pages router handbook
  • alpine.js handbook
  • htmx handbook
  • react handbook
  • sql handbook
  • git cheat sheet
  • laravel handbook
  • express handbook
  • swift handbook
  • go handbook
  • php handbook
  • python handbook
  • cli handbook
  • c handbook

subscribe to my newsletter to get them

Terms: by subscribing to the newsletter you agree the following terms and conditions and privacy policy. The aim of the newsletter is to keep you up to date about new tutorials, new book releases or courses organized by Flavio. If you wish to unsubscribe from the newsletter, you can click the unsubscribe link that's present at the bottom of each email, anytime. I will not communicate/spread/publish or otherwise give away your address. Your email address is the only personal information collected, and it's only collected for the primary purpose of keeping you informed through the newsletter. It's stored in a secure server based in the EU. You can contact Flavio by emailing flavio@flaviocopes.com. These terms and conditions are governed by the laws in force in Italy and you unconditionally submit to the jurisdiction of the courts of Italy.

Related posts about js: