JavaScript Program to Get Random Item From an Array

To understand this example, you should have the knowledge of the following JavaScript programming topics:


Example: Get Random Item From an Array

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 by Math.random().
  • This random index is then used to access a random array element.