Get Random Item From an Array
In this example, you will learn to write a JavaScript program that will get a random item from an array.
To understand this example, you should have the knowledge of the following JavaScript programming topics:
JavaScript Array length
JavaScript Math random()
JavaScript Math floor()
Example: Get Random Item From an Array
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.
Post a Comment