javascript random alphanumeric string code example

Example 1: generate random alphanumeric string javascript

Math.random().toString(36).substr(2, 6); //6 is the length of the string

Example 2: javascript random alphanumeric string

// n is an integer
function randomString (n) {
    // create array of length n filled with null
    let tmpArr = Array.apply(null, Array(n));
    
    // for each index generate a random positive integer
    // less than 36 and convert it to a base 36 digit
    tmpArr.map((v, i) =>
        Math.floor(Math.random() * 36).toString(36)
    ).join('');

    return tmpArr;
}

Tags:

Misc Example