EN
JavaScript - get random item from array
3
points
In this article, we would like to show you how to get random item from array using JavaScript.
Practical example
To get a random item from an array, we use random() and floor() methods from Math built-in object. It is just necessary to random index in the range from 0 to array.length - 1.
// ONLINE-RUNNER:browser;
var array = ['A', 'B', 'C'];
var randomItem = array[Math.floor(array.length * Math.random())];
console.log(randomItem);