Example 1: Generate Random Strings
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
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.