<script> var allItems = ['A', 'B', 'C', 1, 2, 3, 4, 5]; </script>
$thirdItem = allItems;
Next we take a look at a simple way to get a random item from the array by using the
Math.random() function for the index of our array. This function returns a random value between 0 and 1. So in order to get a random value within the range of our arrays indices, we must multiply
Math.random() with the size of our array.
O.K., now we have a number between 0 and 8 (the size of our array in our example). But it’s not an integer. So we’ll have to get an integer value by using the
<script> var allItems = ['A', 'B', 'C',1, 2, 3, 4, 5]; var randomItem = allItems[Math.floor(Math.random()*allItems.length)]; alert(randomItem); </script>