💾arr.randomElement

random element in array (undefined if array is empty)

// ⭐️ arr.randomElement
Object.defineProperty(Array.prototype, 'randomElement', {
    get() { return this[randomInt(this.length)] }
});

// ⭐️ randomElement(arr)
function randomElement(arr){
    return arr[randomInt(arr.length)];
}

Last updated