javascript
Published on January 26, 2024By DeveloperBreeze
// Function to generate a random password of specified length
function generateRandomPassword(length) {
const charset = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*';
let password = '';
// Loop to randomly select characters from the charset
for (let i = 0; i < length; i++) {
const randomIndex = Math.floor(Math.random() * charset.length);
password += charset[randomIndex];
}
return password;
}
// Usage: Generate a random password with a length of 12
let randomPassword = generateRandomPassword(12);
Comments
Please log in to leave a comment.