javascript get random array value

77

pick a random element from an array javascript -

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

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

define function to get random value from array -

const randomValue = (list) => {
    return list[Math.floor(Math.random() * list.length)];
};

Comments

Submit
0 Comments