random item from list javascript code example
Example 1: how to get a random element of an array javascript
var foodItems = ["Bannana", "Apple", "Orange"];
var theFood = foodItems[Math.floor(Math.random() * foodItems.length)];
Example 2: Javascript get random item from array
var colors = ["red","blue","green","yellow"];
var randomColor = colors[Math.floor(Math.random()*colors.length)];
Example 3: get random item from array javascript
const randomElement = array[Math.floor(Math.random() * array.length)];
Example 4: random item from array javascript
function RandomItemFromArray(myArray) {
return myArray[Math.floor(Math.random() * myArray.length)]
}
var fruit = [ "Apples", "Bananas", "Pears", ];
let fruitSample = RandomItemFromArray(fruit)