Random Element

Returns a random element from an array.

Contributed by @itsbrunodev

javascript
function randomItem(arr) {
  return arr[Math.floor(Math.random() * arr.length)];
}
javascript
randomItem([1, 2, 3, 4, 5]); // 3

Keywords

array
random