Get the App
SLTechnology News&Howtos  ›  Development  › 

How to get random items by JavaScript

Shulou Source: shulou.com Published: 2022-06-01 04:25:54 09月13日 Update

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!

Tags: Content array more articles random numbers generation good practical maximum minimum article method maximum useful look knowledge scope project reference help Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information macOS Microsoft OPPO Reno Shulou Tech Info