javascript get random letter code example

Example 1: javascript get random string

function getRandomString(length) {
    var randomChars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
    var result = '';
    for ( var i = 0; i < length; i++ ) {
        result += randomChars.charAt(Math.floor(Math.random() * randomChars.length));
    }
    return result;
}

//usage: getRandomString(20); // pass desired length of random string

Example 2: javascript random char

const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
function generateRandomCode(){
    let result = ""
    let charactersLength = characters.length;
    for ( var i = 0; i < 6 ; i++ ) {
        result += characters.charAt(Math.floor(Math.random() * charactersLength));
    }
    return result
}