create a password with a custom length and lowercase letters in javascript

index.tsx
function generatePassword(length) {
  var password = '';
  var possibleChars = 'abcdefghijklmnopqrstuvwxyz';
  for (var i = 0; i < length; i++) {
    password += possibleChars.charAt(Math.floor(Math.random() * possibleChars.length));
  }
  return password;
}

var password = generatePassword(10); // Generates a password of length 10
console.log(password); // Logs the randomly generated password to the console
412 chars
12 lines

The generatePassword() function takes in a length parameter which is the desired length of the password. It then creates a string of possible characters containing only lowercase letters. The function then loops through length number of times and adds a random character from the possibleChars string to the password string.

This function can be easily modified to include uppercase letters, numbers, and special characters in the password if desired.

gistlibby LogSnag