Tom Lehman Tom Lehman - 1 year ago 107
Javascript Question

Generate random string/characters in JavaScript

I want a 5 character string composed of characters picked randomly from the set


What's the best way to do this with JavaScript?

Answer Source

I think this will work for you:

function makeid()
    var text = "";
    var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

    for( var i=0; i < 5; i++ )
        text += possible.charAt(Math.floor(Math.random() * possible.length));

    return text;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download