create a password with uppercase, lowercase letters, numbers and symbols in typescript

function generatePassword(length: number): string {
  const lowercase = "abcdefghijklmnopqrstuvwxyz";
  const numbers = "0123456789";
  const symbols = "!@#$%^&*()_+-={}[]|\\:;\"'<>,.?/";
  const allChars = uppercase + lowercase + numbers + symbols;
  let password = "";
  for (let i = 0; i < length; i++) {
    const randomIndex = Math.floor(Math.random() * allChars.length);
    password += allChars[randomIndex];
  return password;
500 chars
17 lines


  • The generatePassword() function takes a length argument that specifies the length of the password to be generated.
  • The uppercase, lowercase, numbers and symbols variables hold strings containing the valid characters for each category.
  • The allChars variable concatenates all of the valid characters into a single string.
  • The function then iterates length number of times, randomly selecting a character from allChars each time and appending it to the password variable.
  • Finally, the password variable is returned as the generated password.

gistlibby LogSnag