JavaScript random UUID

// get random uuid
const uuid = crypto.randomUUID();

This created a version-4 (randomly generated) ID like this: 14a3b208-c496-4888-93b9-e3bfa160e5d8.

Crypto: randomUUID() method - Web APIs | MDN

The likelyhood of duplicate values is diminishingly small:

The number of random version-4 UUIDs which need to be generated in order to have a 50% probability of at least one collision is 2.71 quintillion.

This number is equivalent to generating 1 billion UUIDs per second for about 86 years.

— Abridged from Wikipedia

Shorter UUIDs

You can also get short UUIDs with packages like short-uuid - npm or nanoid - npm. According to uuid.rocks these IDs are as unique and collision free as v4 UUIDs because they are converted from them. The short UUIDs use a bigger alphabet including uppercase characters.

Related: