Pick a random item from an array.
If you pass an empty list, then this function will return undefined.
undefined
const myList = [1, 2, 3, 4];const randomItem = sample(myList);console.log(randomItem); // could be 1, 2, 3, or 4console.log(sample([])); // undefined Copy
const myList = [1, 2, 3, 4];const randomItem = sample(myList);console.log(randomItem); // could be 1, 2, 3, or 4console.log(sample([])); // undefined
Pick a random item from an array.