skip to content
VA Logo

Shuffle elements in a JavaScript array

How to shuffle elements in a JavaScript array in a different order

Init list

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

Result

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

and after several calls to the function shuffled()

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