DeveloperApril 2026 ยท 6 min read

How to Generate a UUID in JavaScript

Generating unique identifiers is one of the most common tasks in web development โ€” for database records, API resources, session tokens, and temporary file names. JavaScript now has a built-in UUID generator that works in both browsers and Node.js, no library required.

๐Ÿ†”
Try the UUID Generator
Free, no signup
โ†’
DG
Derek Giordano
Designer & Developer
In this guide
01The Modern Way: crypto.randomUUID()02Fallback for Older Environments03The uuid npm Package04When to Use Each Version
โšก Key Takeaways
  • Generate UUID v4 and v7 in JavaScript using crypto.
  • The Modern Way: crypto.randomUUID().
  • Covers fallback for older environments.
  • The uuid npm Package.
  • When to Use Each Version.

The Modern Way: crypto.randomUUID()

Since 2021, all modern browsers and Node.js 19+ support crypto.randomUUID():

const id = crypto.randomUUID();

// '550e8400-e29b-41d4-a716-446655440000'

This generates a cryptographically random UUID v4 โ€” the format used by most applications. It's fast, secure, and requires no dependencies. This is the recommended approach for all new projects.

Fallback for Older Environments

If you need to support older browsers or Node.js versions before 19, use the Web Crypto API directly:

๐Ÿ’ก Tip
Use 3+ color stops instead of 2 to avoid the muddy gray band that appears in the center of complementary-color gradients.

function uuidv4() {

return '10000000-1000-4000-8000-100000000000'

.replace(/[018]/g, c => (

c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4

).toString(16));

}

This uses crypto.getRandomValues() for cryptographic randomness โ€” never use Math.random() for UUIDs, as it doesn't provide sufficient entropy and can produce collisions.

The uuid npm Package

For projects that need UUID v1 (timestamp-based), v3 (namespace + MD5), v5 (namespace + SHA-1), or v7 (timestamp + random), the uuid npm package is the standard:

โš  Warning
CSS gradients used as backgrounds cannot be animated with standard transitions. Use background-size animation or @property registered custom properties instead.

import { v4, v7 } from 'uuid';

const id = v4(); // random UUID

const sortable = v7(); // time-sorted UUID

UUID v7 is increasingly preferred for database primary keys because it's time-sorted, which improves database index performance compared to random v4 UUIDs.

When to Use Each Version

Use UUID v4 for most cases โ€” session IDs, temporary identifiers, and any context where sort order doesn't matter. Use UUID v7 for database primary keys where time-based sorting improves query performance. Avoid UUID v1 (it exposes the machine's MAC address). For bulk generation or testing, the UUID Generator tool creates multiple UUIDs instantly in any version.

Frequently Asked Questions

How do I generate a UUID in JavaScript?+
Use crypto.randomUUID() โ€” it's built into all modern browsers and Node.js 19+. No library needed. It generates a cryptographically random UUID v4.
Is crypto.randomUUID() safe for production?+
Yes. It uses the same cryptographic random number generator as the Web Crypto API. The generated UUIDs have 122 bits of randomness, making collisions practically impossible.
Should I use UUID v4 or v7?+
Use v4 for general-purpose identifiers. Use v7 for database primary keys โ€” its timestamp prefix makes inserts sequential, which significantly improves database index performance.
Try it yourself

Use the UUID Generator โ€” free, no signup required.

โšก Open UUID Generator
DG
Derek Giordano
Written by the creator of Ultimate Design Tools. BA in Business Marketing.