Generate a random password with JavaScript
Let's generate random passwords using JavaScript's Math.random() and toString methods, along with some array manipulation
By: Ajdin Imsirovic 09 January 2021
It would be interesting if we had a method in JavaScript that would automatically build passwords. Something like a built-in makePassword()
method in vanilla JS. There isn’t. But, it’s not a big deal: we can easily whip up our own password generator in vanilla JS. This tutorial shows you how to do it, step by step, with each step explained in detail.
The requirement for our password is simple:
- It needs to be longer than 8 characters
- It needs to have uppercase letters
- It needs to have lowercase letters
- It needs to have numbers
Let’s start with a quick way to generate a password:
The above code will return, for example:
But, that’s a decimal. So how do we get rid of the first two characters? Easily, using the slice()
method.
The slice method works like this: we pass it a number X, and it removes X characters from the left of the string.
For example:
The slice()
method doesn’t “care” that there is a 0
and a .
in our three-character strings above. It just takes the first two characters and removes them. Thus, if we pass it 0.orrq36ai09
, it will remove the first two characters:
To reiterate, if we did just:
… we’d get something like:
With addition of slice(2)
, we’d get:
And of course, we can pass the number value to toString()
method for various number systems:
- 8 for octal
- 16 for hexadecimal
- 36 for alphadecimal
What if we pass the number 2
to the toString()
method?
We’ll get back a binary number result:
The toString()
method accepts a range of numbers from 2
to 36
as its parameters.
Let’s go back to our random password generator.
1
2
3
4
5
6
7
let pw = Math.random().toString(36).slice(2);
console.log(pw);
/*
returns:
mba01xdkkcg
*/
What if we wanted to make it more random? Could we add some uppercase characters?
Here’s a quick way to do it:
1
2
3
4
5
6
7
let pw2 = Math.random().toString(36).toUpperCase().slice(2);
console.log(pw2);
/*
returns:
J0KJN3QPZ5
*/
Now, how do we make it so that we have a combination of lower and upper characters? We can combine these two arrays, like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
let pw = Math.random().toString(36).slice(2);
pw = [...pw];
console.log(pw);
let pw2 = Math.random().toString(36).toUpperCase().slice(2);
pw2 = [...pw2];
console.log(pw2);
for(let i = 0; i < pw.length; i+=2) {
pw.splice(i,1, pw2[i] );
}
console.log(pw);
Here’s the result of the above code:
Now we have the issue of the length of these arrays resulting in undefined
being added to the resulting array. To solve that, as usual, we can take many approaches, but here’s an interesting one. We’ll use the Array.prototype.length
property to make arrays equal length:
This will immediately trim the extra members from the array that has an extra member. Let’s try our code again:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
let pw = Math.random().toString(36).slice(2);
pw = [...pw];
console.log(pw);
let pw2 = Math.random().toString(36).toUpperCase().slice(2);
pw2 = [...pw2];
console.log(pw2);
if (pw.length > pw2.length) {
pw.length = pw2.length;
} else {
pw2.length = pw.length;
}
console.log('pw length, pw2 length:', pw.length, pw2.length);
for(let i = 0; i < pw.length; i+=2) {
pw.splice(i,1, pw2[i] );
}
console.log(pw);
Here’s a sample output now:
Here’s another sample output:
Our code is almost completed now. All we need to do is join the array back into a string.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
let pw = Math.random().toString(36).slice(2);
pw = [...pw];
console.log(pw);
let pw2 = Math.random().toString(36).toUpperCase().slice(2);
pw2 = [...pw2];
console.log(pw2);
if (pw.length > pw2.length) {
pw.length = pw2.length;
} else {
pw2.length = pw.length;
}
console.log('pw length, pw2 length:', pw.length, pw2.length);
for(let i = 0; i < pw.length; i+=2) {
pw.splice(i,1, pw2[i] );
}
pw = pw.join("");
console.log(pw);
Here’s a sample resulting pasword:
Note:
This exercise comes from Book 3
of my book series on JS, available on Leanpub.com.