JavaScript program to generate random string

Views: 205   

Following program shows you how to generate random string.
This program gets string length from user and prints random string of the given length

var stringLength = prompt("Please enter the string length:")
var alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
var result = '';
for (var i = 1; i <= stringLength; i++) {
result = result + alphabet.charAt(Math.floor(Math.random() * alphabet.length))
console.log("Random string is: " + result);


Please enter the string length:
On By