create a password with uppercase, lowercase letters, numbers and symbols in javascript

You can generate a random password in JavaScript that includes uppercase letters, lowercase letters, numbers, and symbols by using the Math.random() function along with the String.fromCharCode() method.

Here's an example function that generates a random password of a given length:

index.tsx
function generatePassword(length) {
  var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()_+~`|}{[]:;?><,./-=";
  var password = "";
  for (var i = 0; i < length; i++) {
    password += chars.charAt(Math.floor(Math.random() * chars.length));
  }
  return password;
}
300 chars
9 lines

In this function, the chars variable contains all the possible characters that can be used in the password. The for loop iterates over the specified length of the password, selecting a random character from the chars string and adding it to the password variable. Finally, the password variable is returned.

You can call this function with a desired length parameter, like this:

index.tsx
var password = generatePassword(12);
console.log(password); // Example output: "5Uc%7o2M#f@K"
94 chars
3 lines

This will generate a random password with 12 characters that includes uppercase letters, lowercase letters, numbers, and symbols.

gistlibby LogSnag