DEV Community

Cover image for Generate a random alphanumeric string using JavaScript
Thomas Rigby
Thomas Rigby

Posted on • Originally published at thomasxbanks.com on

4 3

Generate a random alphanumeric string using JavaScript

Today I had to send an "ID" in the body of my POST request. It turns out that the API doesn't care what the value is - it just needs to be alphanumeric (a-z, A-Z, 0-9), exactly 18 characters long, and not already in the system.

So I decided to generate a random string*!

Shut up already and give me the code!

const randomString = (len = 1) => new Array(len*2).fill(1000).map((x) => Math.ceil(x * Math.random()).toString(36).charAt(0)).filter(Boolean).sort(() => Math.random() - 0.5).map((x,i) => i % 2 === 0 ? x.toUpperCase() : x).join('').substr(0,len)
Enter fullscreen mode Exit fullscreen mode

Now, let's break that down!

There's a lot going on in that one line!

Firstly, we create an Array with twice as many "characters" as we need.

Then we fill each item in the array with the number 1000.

For each of those items (.map()), we randomise the number by multiplying it by a randomly generated number between 0 and 1 (x * Math.random()). This is then converted into a two-character string. We only need the first character so let's grab that with charAt(0). You could also use [0] as shorthand.

Now we have an Array of twice as many random alphanumeric characters as we asked for.

Just to be on the safe side, we remove any empty or falsey values using .filter(Boolean) - this will get rid of any zeroes but meh 🤷‍♀️

The remaining values are shuffled into a new random order with .sort(() => Math.random() - 0.5).

Our new array is then looped through again and every even-index character is UPPERCASED (if possible - the numbers 0 to 9 cannot be uppercased).

Now we have an array of much more random characters, but this is randomString - what use is an array?! We need to join('') the array together. The '' separator is necessary - without it our string would be full of commas - gross!

Finally, remember how the string is twice as long as we need? - we return the correct number of characters from the start of the string.

Conclusion

So there it is! I have no idea how well it performs but you probably shouldn't be generating random strings in production anyway! 😬

What's this is probably most useful for is playing around with the individual components it's made up of:

  • Fill: Array.fill()
  • Shuffle: Array.sort(() => Math.random() - 0.5)
  • Modulos: i % 2 === 0
  • Remove 'falsey' values: Array.filter(Boolean)

See the Pen Generate a random Alphanumeric String using JavaScript by thomas×banks (ツ) (@thomasxbanks) on CodePen.

* A better solution to this problem is…

const id = btoa(new Date()).substr(0,18);
Enter fullscreen mode Exit fullscreen mode

…but where's the fun in that?! 😈

Top comments (0)

Image of PulumiUP 2025

Let's talk about the current state of cloud and IaC, platform engineering, and security.

Dive into the stories and experiences of innovators and experts, from Startup Founders to Industry Leaders at PulumiUP 2025.

Register Now

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, cherished by the supportive DEV Community. Coders of every background are encouraged to bring their perspectives and bolster our collective wisdom.

A sincere “thank you” often brightens someone’s day—share yours in the comments below!

On DEV, the act of sharing knowledge eases our journey and forges stronger community ties. Found value in this? A quick thank-you to the author can make a world of difference.

Okay