--- title: How to get random items of an array in JavaScript tags: - JavaScript - ES6 - one-line - js - Random layout: note --- In this Article we will go through how to get random items of an array only using single line of code in JavaScript. This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.

Let's define this short function: ```js {.wrap} const randomItems = (arr, count) => arr.concat().reduce((p, _, __, arr) => (p[0] < count) ? [p[0] + 1, p[1].concat(arr.splice(Math.random() * arr.length | 0, 1))] : p, [0, []])[1]; ``` ### Example ```js {.wrap} randomItems([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 3); // [4, 8, 5] randomItems(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'], 4); // ['e', 'c', 'h', 'j'] ```