Get a random item from a JavaScript array

James picture James · May 6, 2011 · Viewed 865.9k times · Source
var items = Array(523, 3452, 334, 31, ..., 5346);

How do I get random item from items?

Answer

Kelly picture Kelly · May 6, 2011
var item = items[Math.floor(Math.random() * items.length)];