JavaScript Program to Generate Random String

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
const characters ='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';

function generateString(length) {
    let result = ' ';
    const 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

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

Output

gyjvo

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

The Math.random() method generates the random number between 0 and 1.

In toString(36) method, 36 represents base 36. The toString(36) represents digits beyond 9 by letters.

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.


Also Read:

Before we wrap up, let's put your understanding of this example to the test! Can you solve the following challenge?

Challenge:

Write a function to find the occurrence of a character in a string.

  • Return the number of times ch appears str.
  • For example, if given str = "programming" and ch = 'm', the number of times 'm' appears in str is 2.
Did you find this article helpful?

Our premium learning platform, created with over a decade of experience and thousands of feedbacks.

Learn and improve your coding skills like never before.

Try Programiz PRO
  • Interactive Courses
  • Certificates
  • AI Help
  • 2000+ Challenges