How to shuffle an array in JavaScript?

To shuffle an array in JavaScript, do this: array.sort(() => 0.5 - Math.random()).

Here's how you do it:

const array = [1, 2, 3, 4, 5, 6];
const shuffled = array.sort(() => 0.5 - Math.random());

console.log(shuffled); // [5, 2, 6, 4, 1, 3]