Skip to content

Shuffle an array in random order with JavaScript

Advertisements

Shuffle an array in random order with JavaScript with this code snippet.

const shuffledArray = (arr) => arr.sort(() => 0.5 - Math.random());

console.log(shuffledArray([1, 2, 3, 4, 5]));
See also  Convert an array of elements into a HTML list in JavaScript

Leave a Reply

Your email address will not be published. Required fields are marked *

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