The task is to select the random element from the array using JavaScript.
Approach 1:
- Use Math.random() function to get the random number between(0-1, 1 exclusive).
- Multiply it by the array length to get the numbers between(0-arrayLength).
- Use Math.floor() to get the index ranging from(0 to arrayLength-1).
Example: This example implements the above appraoach.
<!DOCTYPE HTML> <html> <head> <title> How to select a random element from array in JavaScript ? </title> </head> <body style = "text-align:center;"> <h1 style = "color:green;"> GeeksForGeeks </h1> <p id = "GFG_UP" style = "font-size: 15px; font-weight: bold;"> </p> <button id = "button" onclick = "GFG_Fun()"> click here </button> <p id = "GFG_DOWN" style = "font-size: 24px; font-weight: bold; color: green;"> </p> <script> var up = document.getElementById('GFG_UP'); var down = document.getElementById('GFG_DOWN'); var arr = ["GFG_1", "GeeksForGeeks", "Geeks", "Computer Science Portal"]; up.innerHTML = "Click on the button to check " + "the type of element.<br><br>" + arr; function GFG_Fun() { down.innerHTML = arr[Math.floor(Math.random() * arr.length)]; } </script> </body> </html> |
chevron_right
filter_none
Output:
- Before clicking on the button:

- After clicking on the button:

Approach 2:
- The random(a, b) method is used to generates a number between(a to b, b exclusive).
- Taking the floor value to range the numbers from (1 to arrayLength).
- Subtract 1 to get the index ranging from(0 to arrayLength-1).
Example: This example implements the above approach.
<!DOCTYPE HTML> <html> <head> <title> How to select a random element from array in JavaScript ? </title> </head> <body style = "text-align:center;"> <h1 style = "color:green;"> GeeksForGeeks </h1> <p id = "GFG_UP" style = "font-size: 15px; font-weight: bold;"> </p> <button id = "button" onclick = "GFG_Fun()"> click here </button> <p id = "GFG_DOWN" style = "font-size: 24px; font-weight: bold; color: green;"> </p> <script> var up = document.getElementById('GFG_UP'); var down = document.getElementById('GFG_DOWN'); var arr = ["GFG_1", "GeeksForGeeks", "Geeks", "Computer Science Portal"]; up.innerHTML = "Click on the button to select" + " random element from the" + " array.<br><br>" + arr; function random(mn, mx) { return Math.random() * (mx - mn) + mn; } function GFG_Fun() { down.innerHTML = arr[Math.floor(random(1, 5))-1]; } </script> </body> </html> |
chevron_right
filter_none
Output:
- Before clicking on the button:

- After clicking on the button:

Recommended Posts:
- How to set the value of a select box element using JavaScript?
- How to select Min/Max dates in an array using JavaScript ?
- How to pick a random color from an array using CSS and JavaScript ?
- How to create an array with random values with the help of JavaScript ?
- How to select all children of an element except the last child using CSS?
- How to set the default value for an HTML <select> element?
- How to remove options from select element using jQuery ?
- How to select an element by name with jQuery?
- How to add options to a select element using jQuery?
- How to select first element in the drop-down list using jQuery ?
- How to select html element using ID in jQuery when ID contains a dot character ?
- How to get N options from the <select> element using JQuery?
- How to select an element with multiple classes using jQuery ?
- How to select an element by its class name in AngularJS ?
- How to select all text in HTML text input when clicked using JavaScript?
- How to use Checkbox inside Select Option using JavaScript ?
- JavaScript Math random() Method
- JavaScript to generate random hex codes of color
- JavaScript | Random
- How to generate random number in given range using JavaScript?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


