QuickSnip LogoA light blue logo with a thunderbolt in bracketsQuickSnip

Random string

Generates a random string of characters of a certain length

  • function
  • random

Install

npx shadcn@latest add https://quicksnip.dev/r/random-string.json

This snippet is not verified yet. Review it before use, or help verify it.

Code

function makeid(length, characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789') {
  return Array.from({ length }, () => characters.charAt(Math.floor(Math.random() * characters.length))).join('');
}

makeid(3); // Returns: gDs (Random)
makeid(5, "1234" /* (optional) */); // Returns: "35453" (Random)