const list = [1, 2, 3, 4, 5, 6];
// shuffle your list with the sort function:const shuffledList = list.sort(() =>Math.random() - 0.5);
// generate a size for the new listconst newListSize = Math.floor(Math.random() * list.length)
// pick the first "newListSize" elements from "shuffledList"const newList = shuffledList.slice(0, newListSize)
// [3, 2, 6]; ; [4, 1, 2, 6, 3, 5]; ; etc..
var myArray = [
var randomItem = myArray[Math.floor(Math.random()*myArray.length)];
In this page (written and validated by A. Gawali) you learned about
Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: firstname.lastname@example.org.