[Snippet] Generate Random String with Javascript

When creating a program, sometimes we need a useful random string to create a unique invoice, but we do not have to create it manually. The easiest and most appropriate way is to submit the task to the program we are designing.

random string

One way is with a random string. That is a special function that produces a random string, which is unique and automatically. Since I use javascript, then I use this function to generate a random string.

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;

This function will generate a random string of 5 characters. Quite easy right?

What do you think?

0 points
Upvote Downvote

Written by Bimo Hery Prabowo

Web developer, big fan of CakePHP, loving wordpress soo much. You can found him on Google+ and Facebook. Dont forget to follow him on Twitter.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.