How to Generate n-Character Random Alphanumeric Strings in JavaScript

Tadashi Shigeoka ·  Tue, May 4, 2021

I’ll introduce how to generate n-character random alphanumeric strings in JavaScript.

JavaScript

JavaScript Examples for Generating n-Character Random Alphanumeric Strings

3-Character Random Alphanumeric String Generation Sample Code (Vanilla JavaScript)

When running JavaScript in Zapier, you cannot use crypto, so please use the following sample code.

var N = 3;
var randStr = Math.random().toString(36).slice(-N);

3-Character Random Alphanumeric String Generation Sample Code (For Browser Operation)

var S = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
var N = 3
Array.from(crypto.getRandomValues(new Uint8Array(N))).map((n)=>S[n%S.length]).join('')

3-Character Random Alphanumeric String Generation Execution Results

The results of executing the above sample code are as follows (executed 5 times):

"RTC"
"T9E"
"85X"
"8B7"
"LR5"

That’s all from the Gemba about generating n-character random alphanumeric strings in JavaScript.

Reference Information