Answers for "how to get random string element from array in javascript"

1

pick random string from array javascript

var groceries = [
'milk',
'coriander',
'cucumber',
'eggplant'
]
let mygroceries = groceries[Math.floor(Math.random() * groceries.length)]
console.log(mygroceries)//This gives you any string from groceries
Posted by: Guest on June-14-2020
0

javascript random text from array

var textArray = [
    'song1.ogg',
    'song2.ogg'
];
var randomNumber = Math.floor(Math.random()*textArray.length);

audioElement.setAttribute('src', textArray[randomNumber]);
Posted by: Guest on April-05-2021

Code answers related to "how to get random string element from array in javascript"

Code answers related to "Javascript"

Browse Popular Code Answers by Language