Toolbox-XToolbox-X

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): string

Parameters

ParameterTypeDescription
lengthnumber (optional)The desired length of the random string. Default: 8
uppercaseboolean (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 length is 0 or negative, an empty string is returned.
  • Uses crypto.getRandomValues (via randomBytes) when available for secure randomness, and falls back to Math.random if not.
  • The uppercase parameter 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)

On this page