Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

javascript random item of array

// Define random() method of Arrays
Array.prototype.random = function() {
	return this[Math.floor(Math.random() * this.length)];
}

console.log([1, 2, 3, 4, 5, 6].random()); // 5 for example
console.log(['apple', 'banana', 'orange'].random()); // 'orange' for example
Source by www.typescripttutorial.net #
 
PREVIOUS NEXT
Tagged: #javascript #random #item #array
ADD COMMENT
Topic
Name
2+6 =