Generating Random Alpha-Numeric String

In some cases, we need to generate random strings with JavaScript. Since there is no built-in function for that, we need to bake something out as a solution. With the help of array’s reduce function, we can easily generate a random alpha-numeric string with the desired length.

// Generate the random string.
random(length = 10) {
  return Array(length).fill(null).reduce((acc, curr) => acc + Math.floor(Math.random() * 100).toString(36), ''); 
}
Note, we multiply the random number by 100, because the toString() method will convert numbers to characters above 9.
Need a web developer? Maybe we can help, get in touch!