Generate Random Alphanumeric
Generate a random alphanumeric string (letters and numbers) of the specified length.
randomAlphaNumeric
The randomAlphaNumeric function generates a random alphanumeric string (letters and numbers) of the specified length.
Info
Does not rely on Node.js or Web APIs. Works on any JS engine
Function Signature
randomAlphaNumeric(length?: number, uppercase?: boolean): stringParameters
| Parameter | Type | Description |
|---|---|---|
length | number (optional) | The desired length of the random string. Default: 8 |
uppercase | boolean (optional) | If true, the string will contain uppercase letters. Default: false |
Return Value
Returns a randomly generated alphanumeric string composed of letters and numbers of the specified length.
Example Usage
playground.ts
Use Cases
- Generating unique identifiers
- Creating random tokens for authentication or password resets
- Generating temporary session IDs
- Generating readable codes or reference keys
Implementation Notes
- The function generates random bytes and converts them to base-36 (0-9, a-z) characters.
- If
lengthis0or negative, an empty string is returned. - Uses
crypto.getRandomValues(viarandomBytes) when available for secure randomness, and falls back toMath.randomif not. - The
uppercaseparameter controls whether the letters are uppercase or lowercase. By default, it returns lowercase letters.
See Also
- randomHex - Generate cryptographically secure random hex strings
- randomBytes - Generate cryptographically secure random bytes (
Uint8Array) - randomNumeric - Generate cryptographically secure random numeric strings
- randomID - Generate customizable random alphanumeric ID (cryptographically not secure)
Last updated: Tue, Jun 16, 2026 06:17:07PM (UTC)
