javascript function generate a random string 1

javascript function generate a random string

//Create a function that returns a string, of a given length
const genRand = (len) => {
  return Math.random().toString(36).substring(2,len+2);
}

console.log(genRand(8));
//Expected output example: 4yg020wn

Here is what the above code is Doing:
1. Math.random() returns a random number between 0 and 1.
2. toString(36) converts the number to a string, using a base of 36.
3. substring(2, len+2) returns a substring of the string, starting at index 2, and ending at index len+2.

Similar Posts