Skip to content

Instantly share code, notes, and snippets.

@Zorgatone
Last active October 5, 2015 11:43
Show Gist options
  • Select an option

  • Save Zorgatone/1ee94e74f4dfdd962d16 to your computer and use it in GitHub Desktop.

Select an option

Save Zorgatone/1ee94e74f4dfdd962d16 to your computer and use it in GitHub Desktop.
Get random array element, directly from the object
/**
* Get random array element, directly from the object
*/
Array.prototype.random = Array.prototype.random || function random() {
return this[Math.floor(Math.random() * this.length)];
};
@Zorgatone

Copy link
Copy Markdown
Author

Example usage:

console.log([
    "apple",
    "pear",
    "orange"
].random());

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment