How to Generate Random Password in Javascript

In this tutorial, you will learn how to generate random password in javascript. Generally, we end up using the same password for all of our online accounts and this is the thing that makes our online account more vulnerable to attackers.

These days it is extremely important to have a random password for each of your online accounts because it makes your online accounts more secure and less vulnerable to attackers.

There are multiple ways to generate random password in javascript. Creating such random password generator in javascript from scratch can be time-consuming and that is why we are going to use the password-generator library. There are more libraries similar to this, but I found it extremely simple to use and it comes with a bunch of features that can be helpful in generating a random password.

With the help of this library, we can also detect if the password entered by the user is strong enough or not.  We will not cover that part in this tutorial since it is out of the scope of this tutorial, so I will recommend you to have a look over this library’s GitHub repo to learn more about it.

In the following example, we will generate a random password upon click of a button and display that on the screen. Please have a look over the code example and the steps given below.

HTML & CSS

  • We have 3 elements in the HTML file (div, h1, and button). The div element is just a wrapper for the rest of the elements.
  • The innerText for the button element is ”Generate” and for the h1 element, it is “Result”.
  • We have downloaded the password generator library and included that at the bottom of the HTML file using the script tag.
  • We have done some basic styling using CSS and added the link to our style.css stylesheet inside the head element.
  • We have also included our javascript file script.js with a script tag at the bottom.
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
    
    <div>
        <button>Generate</button>
        <h1>Result</h1>
    </div>
    
    <script src="js/password-generator.min.js"></script>
    <script src="js/script.js"></script>
</body>
</html>
div {
    display: flex;
    flex-direction: column;
    align-items: center;
}

button {
    padding: 10px;
}

Javascript

  • We have selected the button element and the h1 element using the document.querySelector() method and stored them in btnGenerate and result variables respectively.
  • We have attached the click event listener to the button element.
  • In the event handler function, we are calling generatePassword() method and passing it 4 parameters. All of these parameters are optional.
  • The first parameter takes a number that represents the length of the password. We are passing 25 here.
  • The second parameter takes a Boolean value that represents whether your password should be memorable or not. A non-memorable password will include numbers and letters whereas a memorable password will have only letters.  We are passing false which means our password will be non-memorable.
  • The third parameter takes a regex pattern and as a result, this library will only generate the password that matches this pattern. We are passing an empty string and that means we do not want our password to match any regex pattern.
  • The fourth parameter takes a string that represents a prefix for the password. We are passing “kodebase-“ and that means the generated password will have this string as a prefix. This string already contains 9 characters so this library will generate 16 characters and simply append them to the prefix to fulfill our minimum 25 characters requirement.
  • We are displaying the generated password in the h1 element using the innerText property.
let btnGenerate = document.querySelector('button');
let result = document.querySelector('h1');

// https://github.com/bermi/password-generator
btnGenerate.addEventListener('click', () => {
   result.innerText = generatePassword(25, false, '', 'kodebase-'); 
});