JavaScript

How to Shuffle an Array Using JavaScript?

JavaScript is a programming-based language that provides interactive websites to its clients using built-in functions. Shuffling is the concept of rearranging the elements of any sorted/sequence order. It has various applications in gaming, cryptography, statistics, etc.

In JavaScript, the shuffle() method is utilized to conduct the shuffling tasks. The method takes input as an array and returns a new array by exchanging the positions of elements in the array.

This post demonstrates the built-in method of JavaScript.

How to Shuffle an Array in JavaScript?

In JavaScript, the shuffle() method is adapted to change the location of elements in an array. It does not modify/edit the array elements. However, it reorders the elements present in the array at each array.sort() call. An array.sort() is employed to sort the elements in the array. It randomly changes the positions of elements in the array.

The shuffle() method takes the list or array as input. After that, it performs a mathematical operation on it and returns the element of the list to rearrange the order.

Example 1: Using the shuffle() Method in JavaScript

An example is employed to shuffle an array using JavaScript. The code is given below:

Code

// An example of shuffle an array in JavaScript
let numbers = [1, 2, 3, 4, 5];
let letters = ["a", "b", "c", "d", "e"];
functionshuffle(array)
{
return array.sort(() =>Math.random() - 0.5);
  }
console.log(shuffle(numbers));  
console.log(shuffle(letters));

The description of the code is as follows in the listed format:

  • Firstly, two arrays, “numbers” and “letters” are used in which five elements are defined.
  • After that, a shuffle() method is employed by passing the array of five elements.
  • In the shuffle() method, the array.sort() is used to return the new array by switching the locations of array elements.
  • In the end, shuffled arrays are displayed using the console.log() method by passing the numbers and letters arrays.

Output

The code returns the two arrays of numbers and letters by changing the position of the elements of each array.

Example 2: Shuffle an Array Using for Loop in JavaScript

An example is considered for shuffling an array using a for loop. For instance, the code is given below.

Code

// Another example of shuffle an array in JavaScript
let array = [1, 2, 3, 4, 5]
for(let x = array.length - 1; x >= 1; x--) {
let y = Math.floor(Math.random() * (x + 1));
let t = array[y];
   array[y] = array[x];
   array[x] = t;
}
console.log(array);

In this code:

  • An array is created with the name of the array having five values.
  • After that, a for loop is initialized with the length of elements. After that, the loop is executed until the value is reached or greater than 1.
  • After that, Math.random() method is employed to generate a number randomly between 1 and 5.
  • Finally, display the array using the console.log() method.

Output

The output shows the execution of the above code and returns the shuffled array using JavaScript.

Conclusion

In JavaScript, the shuffle() method is employed to exchange the positions of elements in an array. The random positioning phenomenon shuffles the elements of the array. The method does not edit the element’s value. It returns a new array in an unsorted order by utilizing the array.sort() method. Here, you have learned to shuffle an array using JavaScript. For a better understanding, we have explained the working of the shuffle() method as well.

About the author

Syed Minhal Abbas

I hold a master's degree in computer science and work as an academic researcher. I am eager to read about new technologies and share them with the rest of the world.