A UUID (Universally Unique Identifier) is a 128-bit label used to uniquely identify objects in computer systems. The standard format is: xxxxxxxx-xxxx-Mxxx-Nxxx-xxxxxxxxxxxx, where M indicates the version and N indicates the variant.
UUID Versions Explained
- UUID v1: Time-based — encodes the current timestamp and MAC address
- UUID v3: Name-based with MD5 hash — deterministic from a namespace + name
- UUID v4: Random — 122 random bits, most commonly used
- UUID v5: Name-based with SHA-1 hash — preferred over v3
When to Use UUID v4
UUID v4 is the right choice for most use cases: database primary keys, session IDs, file names, API request IDs. The collision probability is astronomically low (you would need to generate 1 billion UUIDs per second for 85 years to have a 50% chance of one collision).
Generating UUIDs in JavaScript
// Modern browsers and Node.js 14.17+
const id = crypto.randomUUID();
// Output: "550e8400-e29b-41d4-a716-446655440000"UUID vs ULID vs NanoID
UUID v4 is the universal standard. ULID is sortable by creation time (useful for databases). NanoID is shorter (21 characters) and URL-safe. For most cases, UUID v4 is the best default.