DEV Community

Arhan Khan
Arhan Khan

Posted on

Build a Password Generator App with JavaScript

Every account needs a strong password, and nobody can remember twenty different ones. A password generator is a small, genuinely useful project: it teaches random selection, character sets, and clipboard handling, and you end up with a tool you will actually use.
Start with the character sets:

const SETS = {
  lower: "abcdefghijklmnopqrstuvwxyz",
  upper: "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
  numbers: "0123456789",
  symbols: "!@#$%^&*",
};
Enter fullscreen mode Exit fullscreen mode

Let the user pick a length with a slider and toggle each set on or off. Build the password by picking random characters from the combined pool, and guarantee at least one character from each selected set so the result always meets the rules:

function generatePassword(length, pool) {
  const array = new Uint32Array(length);
  crypto.getRandomValues(array);
  return Array.from(array, (n) => pool[n % pool.length]).join("");
}
Enter fullscreen mode Exit fullscreen mode

Note the use of crypto.getRandomValues instead of Math.random. For passwords, you want cryptographic randomness, and this is the browser API built for it. It is a small detail that shows you understand the difference.
Add a strength meter that estimates entropy from the length and the size of the pool, a copy button that uses the Clipboard API, and a history list that keeps the last few generated passwords on screen until the page is closed — never save them to localStorage, because a password tool should not store passwords.
The limits are honest ones: a generator does not store or manage passwords, so pair it with a password manager. Generated passwords are hard to remember, so they are meant to be copied, not memorised. And the app cannot fix reused passwords — it can only make new, strong ones.
Build it today: length slider, four toggles, generate, copy, and a strength meter. Test it by generating ten passwords and checking that each one contains the character types you selected.

Top comments (0)