How to make a random password generator | Password Generator Js

How to make a random password generator | Password Generator Js

How to make a random password generator | Password Generator Js

Welcome to today’s tutorial. Today we are going to create a Random Password Generator. For this, we are going to use HTML, CSS and Javascript.

 You need basic ES6 knowledge for this javascript project. This tutorial is well suited for javascript intermediates. Let us get started with the project. 


The task is to generate a random password that may consist of alphabets, numbers, and special characters. This can be achieved in various ways in this article we will discuss.



Before moving forward to the code first of all we will see the live sever of the code so you can see how it is working on clicking the button.


See the Pen Untitled by Himanshu Singh (@himanishu) on CodePen.


 Make a string consist of Alphabets(lowercase and uppercase), Numbers and Special Characters. the we will use Math.random() and Math.floor() method to generate a number in between 0 and l-1(where l is length of string).
To get the character of the string of a particular index we can use .charAt() method
This will keep concatenating the random character from the string until the password of the desired length is obtained.

I have used inline CSS For Styling. 
With the help of inner HTML property of the DOM , I have inserted at last the password.

Math.random() returns a random number between 0 (inclusive),  and 1 (exclusive).
The Math.floor() function returns the largest integer less than or equal to a given number.
We have used internal Javscript and internal CSS in this project

Generate a Random Password
using JavaScript
<body style="text-align:center;">
<h1 style="color: red">
Random Password generator
Click on the button to
generate random password.
<button onclick="Run()">
Click Here
<p id="pass"></p>
var el_down = document.getElementById("pass");
/* Function to generate combination of password */
function generateP() {
var pass = '';
for (i = 1; i <= 8; i++) {
var char = Math.floor(Math.random()
* str.length + 1);
pass += str.charAt(char)
return pass;
function Run() {
el_down.innerHTML = generateP();


I hope you have loved this blog and learnt many things at a place please let us know your review in the comment section if you liked it please comment below as it will give us motivation to create more blogs.

If you faced any difficulty feel free to comment down your problems and If you really liked it, please show your love in the comment section this really motivates a blogger to provide more such content.

You can follow me on Instagram.

Written by @Himanshu Singh.

Leave a Reply