Do you mean get a random array?
var strings = ['a', 'b', 'c']; var randomIndex = Math.floor(Math.random() * strings.length); var randomString = strings[randomIndex];
Take a look at jsFiddle .
If you mean a random string, it is a little different.
var randomStrLength = 16, pool = 'abcdefghijklmnopqrstuvwxyz0123456789', randomStr = ''; for (var i = 0; i < randomStrLength; i++) { var randomChar = pool.charAt(Math.floor(Math.random() * pool.length)); randomStr += randomChar; }
Take a look at jsFiddle .
Of course, you can skip the pool variable and make String.fromCharCode() with a random number between 97 ( 'a'.charCodeAt(0) ) and 122 ( 'z'.charCodeAt(0) ) for lowercase letters, etc. But depending on the range you want (lowercase and uppercase, as well as special characters), using the pool is less complicated.
source share