We will go through two different method by which we can easily pick a random element from an array. So let’s get started.
Using the Random(a, b) method
In this case, we will take the value of starting number as 0 and the ending number as the length of the array. When a random number is generated, we need to take the floor value to range the numbers from (1 to array length). Here is an example of the same.
var rand = myArray[Math.floor(Math.random() * myArray.length)];
Using the lodash Library
_.sample(['January', 'February', 'March']);