How to Generate Random Password in Javascript

 

In this video tutorial, you will learn how to generate random password in javascript.  There are multiple ways to generate random password, but I found this easy to use password=generator library. It comes with a bunch of features which can be helpful in generating a random password.

This library also gives you ability to detect if the password entered by user is strong enough or not.  That is out of scope of this tutorial, so I will recommend you to have a look over their gtihub repo to learn more about this library.

HTML:

<!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>

 

CSS:

div {
    display: flex;
    flex-direction: column;
    align-items: center;
}

button {
    padding: 10px;
}

 

Javascript:

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-'); 
});

 

 

  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •