get a random item from a javascript array code example
Example 1: javascript get random array value
var colors = ["red","blue","green","yellow"];
var randColor = colors[Math.floor(Math.random() * colors.length)];
Example 2: js get random from array
Array.prototype.random = function() {
return this[Math.floor(Math.random() * this.length)];
}
var result = ["Hello", "world"].random();
Example 3: how to get a random statement from an array in javascript
let Testing1 = ["put","things","here"]
let Generate = Math.floor((Math.random() * Testing1.length));
console.log(Testing1[Generate])
Example 4: get random elements from array javascript
array.sort(() => Math.random() - Math.random()).slice(0, n)