Example: Get Random Item From an Array
// program to get a random item from an array
function getRandomItem(arr) {
// get random index value
const randomIndex = Math.floor(Math.random() * arr.length);
// get random item
const item = arr[randomIndex];
return item;
}
const array = [1, 'hello', 5, 8];
const result = getRandomItem(array);
console.log(result);
Output
'hello'
In the above program, a random item from an array is accessed.
- A random number between 0 to array.length is generated using the
Math.random()
method. - The
Math.floor()
returns the nearest integer value generated byMath.random()
. - This random index is then used to access a random array element.