How to get random items by JavaScript
This article is about how JavaScript gets random items. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Get random item
Generating random numbers or getting random items from an array is a very useful and convenient method. I have seen them many times in many of my projects.
Get random items from an array
Let planets = ['Mercury', 'Mars',' Venus', 'Earth',' Neptune', 'Uranus',' Saturn', 'Jupiter']; let randomPlanet = planets [Math.floor (Math.random () * planets.length)]; console.log (' Random Planet', randomPlanet)
Generate a random number in a range by specifying the minimum and maximum values
Let getRandom = (min, max) = > {return Math.round (Math.random () * (max-min) + min);} console.log ('Get random', getRandom (0,10)); Thank you for reading! This is the end of the article on "how to get random items in JavaScript". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!