how to get a random element of an array javascript

152

pick a random element from an array javascript -

var myArray = [
  "Apples",
  "Bananas",
  "Pears"
];

var randomItem = myArray[Math.floor(Math.random()*myArray.length)];

javascript how to get a random element from an array -

var items = ['Yes', 'No', 'Maybe'];
var item = items[Math.floor(Math.random() * items.length)];

Comments

Submit
0 Comments