how to choose a random item from a list in javascript code example

Example 1: Javascript get random item from array

var colors = ["red","blue","green","yellow"];
var randomColor = colors[Math.floor(Math.random()*colors.length)]; //pluck a random color

Example 2: how to get a randome element from a list in javascript

let numList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
let randomElem = numList[Math.floor(Math.random() * numList.length)];
console.log(randomElem);

Example 3: get random entry from array javascript

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