shuffle array javascript – How to randomize (shuffle) a JavaScript array?

shuffle array javascript : The shuffle() function randomizes the order of the elements in the array. Randomize or shuffle array javascript Engine, Measure the Randomness of Our Simple Algorithm, using Fisher-Yates Shuffle Algorithm and With the Underscore.js or Lo-Dash Library.

shuffle array javascript – How to shuffle elements in a JavaScript array?

Write the simple JavaScript function shuffle(array) that shuffles (randomly reorders) elements of the array.

Also Read This πŸ‘‰   How to validate multiple checkbox selection in a JavaScript?

function shuffleArray(array) {
  let trendIdx = array.length,  shuffleIdx;

  while (trendIdx != 0) {
    shuffleIdx = Math.floor(Math.random() * trendIdx);

    [array[trendIdx], array[shuffleIdx]] = [
      array[shuffleIdx], array[trendIdx]];

  return array;

// Used like so
var user_rank = [11, 41, 55, 85];

don’t miss : PHP – Array Functions Example With Demo

How to shuffle elements in a JavaScript array?

let user_rank = [11, 12, 13, 14, 15, 16, 17, 18, 19]
user_rank = user_rank.sort(() => Math.random() - 0.5)

How to shuffle an array in JavaScript?

function shuffleArray(inputArray){
    inputArray.sort(()=> Math.random() - 0.5);

var user_rank = [11, 12, 13, 14, 15, 16, 17, 18, 19];

Free Live Chat for Any Issue

I hope you get an idea about shuffle array javascript.
I would like to have feedback on my
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Rate this post