How to create a password generator using JavaScript(Node.js)?

How to create a password generator using JavaScript(Node.js)?

Security is the most important thing these days. Even though the applications have been more secure than ever before, the passwords being used are mostly the same, and easily guessable. Like, password, 12345678, joe1996 and so on.

So, here is the way, how you can easily create a password generator for yourself.

How to create a password generator using JavaScript(Node.js)?

// password_generator.js

const passwordLength = process.argv[2];

const generatePassword = (length) => {
  let randomPassword = "";
  const seed = `abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789~!@#$%^&*()-=+{}[];:'"|\/.,><?/`;
  for (let i = 0; i < length; i++) {
    randomPassword += seed.charAt(Math.floor(Math.random() * seed.length));
  }
  return randomPassword;
};

console.log(generatePassword(passwordLength));

Here, we have the generatePassword function, which generates our password. Inside the function, we have a seed of letters that we want to include in our passwords. Then, we iterate the for loop up to the number, which is the length of the password we want to generate. Each time we iterate, we will have a random letter from our seed. We have also used process.argv to get the passwordLength, expecting the first command line argument to be the length of the password we want to generate. Finally, we call the function and print the generated password.

How to use it?

$ node password_generator.js 8 # password length

How you can further extend it?

  • Support for more command-line arguments

  • Copy to clipboard functionality

Thanks for reading! ❤️