[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?

No Responses

Show all responses

Write a response