Skip to main content
CSS-Tricks
  • Articles
  • Videos
  • Almanac
  • Newsletter
  • Guides
  • DigitalOcean
  • DO Community
Search
Code Snippets → JavaScript → Select Random Item from an Array

Select Random Item from an Array

Avatar of Chris Coyier
Chris Coyier on Dec 23, 2016
var myArray = [
  "Apples",
  "Bananas",
  "Pears"
];

var randomItem = myArray[Math.floor(Math.random()*myArray.length)];

See the Pen gLJPZv by Chris Coyier (@chriscoyier) on CodePen.

Psst! Create a DigitalOcean account and get $200 in free credit for cloud-based hosting and services.

Comments

  1. Adam Lemmo
    Permalink to comment# December 25, 2016

    This is super succicnt, and here I was doing this all the time like a total schmo…

    …

    JS Result
    EDIT ON
    function randomNum(minVal, maxVal) {
    do {
    r = Math.random();
    } while (r == 1);
    return minVal+Math.floor(r*(maxVal+1-minVal));
    }
    var coolwords = new Array();
    coolwords[0] = “robot”;
    coolwords[1] = “inferno”;
    coolwords[2] = “giga”;
    coolwords[3] = “infinity”;
    coolwords[4] = “pow”;
    coolwords[5] = “smash”;
    coolwords[6] = “boom”;
    coolwords[7] = “crunch”;
    coolwords[8] = “robot”;
    coolwords[9] = “inferno”;

    document.body.innerHTML = coolwords[randomNum(0, coolwords.length-1)];

    Reply
  2. Jyoti
    Permalink to comment# December 26, 2016

    Hi guys this is best example I was searching the same. It was difficult to find out value randomly now it becomes very easy. Thanks for sharing.

    Reply
    • Namit
      Permalink to comment# June 13, 2020

      Thanks you very much

  3. Dinesh
    Permalink to comment# December 26, 2016

    Great post guys

    Reply
  4. Pankaj
    Permalink to comment# December 26, 2016

    var mrrandom = Math.floor(Math.random()*10);
    $(“.myclass”).eq(mrrandom).click();

    This is Best example. you can try it.

    Reply
  5. Johan Karlsson
    Permalink to comment# August 4, 2018

    Thank you! As a total newbie developer this actually helped me complete my first (extremely simple and crude) website. Now to understand the Math command… :)

    Reply
  6. Narek
    Permalink to comment# April 1, 2022

    Thank you for an example!
    But I need to say there’s a missing subtraction “- 1”, because in case when our randomizer returns the maximum value “1”, we get myArray[3] — a non-existent element in the array. So we need to substract 1 from the array length in order to stay in the arrays existing indexes boundaries.
    var randomItem = myArray[Math.floor(Math.random()*(myArray.length – 1))];

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

CSS-Tricks is powered by DigitalOcean.

Keep up to date on web dev

with our hand-crafted newsletter

DigitalOcean
  • DigitalOcean
  • DigitalOcean Community
  • About DigitalOcean
  • Legal
  • Free Credit Offer
CSS-Tricks
  • Email
  • Guest Writing
  • Book
  • Advertising
Follow
  • Mastodon
  • Twitter
  • Instagram
  • YouTube
  • CodePen
  • iTunes
  • RSS
Back to Top