How do I get a random item from a JavaScript array?

Hey @prynka.hans , I agree with the other two methods shared , they’re solid and straightforward. But here’s my approach, which is a bit quirky and different:

const items = [523, 3452, 334, 31, 5346];
const randomItem = items.reduce((selected, current) => Math.random() < 1 / items.length ? current : selected);
console.log(randomItem);

This one uses reduce to walk through the array and randomly decide whether to keep the current item as the selected one. It’s not as common, but I find it interesting because it gives every item a fair chance as it iterates. Definitely a fun experiment if you want to try something outside the usual!