<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script> <script type="text/javascript"> $(function(){ var arr = ["red","yellow","blue","green","orange","black"]; var num = selectFn(0,arr.length-1); console.log(num); console.log(arr[num]); }); function selectFn(min,max){ var choices = max-min+1; return Math.floor(Math.random()*choices+min); } </script> </body> </html>