JavaScript Program to Generate Random String

In this example, you will learn to write a JavaScript program that will generate strings of any size by picking characters randomly from A-Z, a-z, and 0-9.

To understand this example, you should have the knowledge of the following JavaScript programming topics:


Example 1: Generate Random Strings

// program to generate random strings

// declare all characters
let characters ='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';

function generateString(length) {
    let result = ' ';
    let charactersLength = characters.length;
    for ( let i = 0; i < length; i++ ) {
        result += characters.charAt(Math.floor(Math.random() * charactersLength));
    }

    return result;
}

console.log(generateString(5));

Output

B5cgH

In the above example, the Math.random() method is used to generate random characters from the specified characters (A-Z a-z 0-9).

The for loop is used to loop through the number passed into the generateString() function. During each iteration, a random character is generated.


Example 2: Generate Random Strings using built-in Methods

// program to generate random strings

let result = Math.random().toString(36).substring(2,7);
console.log(result);

Output

gyjvo

In the above example, the built-in methods are used to generate random characters.

The Math.random() method is used to generate random characters.

The toString(36) represents A-Z a-z 0-9.

The substring(2, 7) method returns five characters.

Note: In the above examples, the output varies each time because random characters are generated at every execution.