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.
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
.
xxxxxxxxxx
1
var array = ['A', 'B', 'C'];
2
3
var randomItem = array[Math.floor(array.length * Math.random())];
4
5
console.log(randomItem);