Last Updated: September 21, 2021
·
2.426K
· kalinin84

JavaScript: Random password

function getRandomPassword(length) {
    const x = '-*+=0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
    const items = [];
    for (let i = 0; i < length; i++) {
        items.push(x[Math.floor(Math.random() * x.length)]);
    }
    for (let i = items.length - 1; i > 0; i--) {
        let j = Math.floor(Math.random() * (i + 1));
        [items[i], items[j]] = [items[j], items[i]];
    }
    return items.join('');
}

3 Responses
Add your response

Generates a strong password, thanks for the tip.

7 months ago ·

If this is for the backend, You can just use the built in crypto library that comes with Node: https://nodejs.org/api/crypto.html

3 months ago ·

Excellent idea to generete a strong password

about 7 hours ago ·