How to shuffle the array? JavaScript

Опубликовано: 07 Май 2026
на канале: Ефим Рябов
1,527
53

Link to publication: https://efim360.ru/javascript-kak-per...

00:00 Start
00:40 ECMAScript Math object. Random () method
01:45 Method random () called in the browser console
02:35 What do we want to do in the first step? Using the map () method
03:10 Let's return pairs as an array
03:50 Got an array from arrays
04:45 Sort the new array using the sort () method
05:35 We go through the array once again using the map () method
07:20 What else? Create a function
08:15 We wrote our own function for shuffling the array
09:00 Extending the standard set of methods for the Array class
10:15 How does your own method work?

Thanks for watching!

Shuffle the array using JavaScript's built-in Math object and its random () method, which returns us a random value from zero to one, but not including 1.

From an array of elements, we create pairs - a random number and an array element. After that we sort the new array of pairs. Sorting is performed by newly generated random numbers. Because of this, the original elements get mixed up.

After that, we iterate over the new array and return only the "old" elements. They will be mixed in the new array.