Since you are newer to JavaScript, let's do it the simplest way. Not the fastest, or shortest way, but the simplest, easiest to understand way.
I believe that combining these arrays:
let upperCase = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"] ;
let underCase = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"] ;
let numbers = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"] ;
into just one:
let chars = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"] ;
will make our job simpler. There is no need to choose a random array and then a random character now. It's just simply choosing a random character.
Now we set an arbitrary length of passwords we wish to generate. Let's say 8 for now.
let passwordLength = 8;
We'll need to store the generated password, so let's also make a variable for that:
let generated = "";
Simply put, we want to add random characters to this generated password however many times we stored in the password length.
Sounds like we need a loop!
We'll use a traditional C-style loop here:
for (let i = 0; i < passwordLength; i++) {
}
and inside the loop, we want to add a random character.
This can be done in many ways, but the most common is this:
chars[Math.floor(Math.random() * chars.length)]
Because Math.random() generates numbers from 0 to 1 but excluding 1, we are making a range from 0 to chars.length, excluding chars.
We only want integers, though, so we use Math.floor (functionally the same as stripping off the decimal part).
Then we use the random index to access the random character in the array.
Finally, we add the random character to the generated password:
generatedPassword += chars[Math.floor(Math.random() * chars.length)]
So when you're all done, it should look something like this:
let chars = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"] ;
let passwordLength = 8;
let generated = "";
for (let i = 0; i < passwordLength; i++) {
generated += chars[Math.floor(Math.random() * chars.length)];
}
console.log(generated);
If you run the snippet, you should see a random password of length 8!
References: