javascript get random character from string code example

Example 1: javascript generate random string

// Generate a random alphanumerical string of length 11 ( change substring parameter for other length)
Math.random().toString(36).substring(2);

Example 2: random alphabet javascript

Math.random().toString(36).substr(2, 5);

Example 3: 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
}