Shuffle an Array (Fisher-Yates)
Shuffle an Array (Fisher-Yates) is a reusable JavaScript snippet. Randomly shuffles the elements of an array using the efficient Fisher-Yates algorithm. The full implementation: function shuffle(arr) { const result = [...arr] for (let i = result.length - 1; i > 0; i--) { const j = Math.floor(Math.random() * (i + 1)) ;[result[i], result[j]] = [result[j], result[i]] } return result } // Usage const cards = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] shuffle(cards) // Randomly shuffled // In-place version function shuffleInPlace(arr) { for (let i = arr.length - 1; i > 0; i--) { const j = Math.floor(Math.random() * (i +… This snippet uses only standard JavaScript with no dependencies, so it runs in modern browsers and Node.js alike. Browse more patterns in the snippet library, or paste the code straight into the playground to experiment with it.