<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Random Item from Array</title>
<h1>Return Random Element from Array Javascript</h1>
<button onclick="showRandomItem()">Show Random Item</button>
<p id="randomArrayDisplay"></p>
<p id="randomItemDisplay"></p>
function getRandomItem(array) {
const randomIndex = Math.floor(Math.random() * array.length);
return array[randomIndex];
function showRandomItem() {
const myArray = [1, 2, 3, 4, 5];
const randomItem = getRandomItem(myArray);
document.getElementById('randomArrayDisplay').textContent = `Array: [${myArray.join(', ')}]`;
document.getElementById('randomItemDisplay').textContent = `Random Item: ${randomItem}`;