Shuffle an array

Sometimes you want to randomize the values within an array. To achieve this you can use the Array.sort function with a random compareFunction.

const array = ["one", "two", "three", "four"]
array.sort(compareFn: function(){ return Math.random() - 0.5})
console.log(array)

Leave a comment

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