Generate UUID v4 and v7 in JavaScript
Copy-paste JavaScript code that produces a UUID v4 (random, the long-standing default) or UUID v7 (time-ordered, the modern choice for database keys). Both blocks below are production-ready.
UUID v4 in JavaScript
v4 is purely random — 122 bits of entropy, no structure. Use it when you don't want timing information embedded in the ID, or when you don't care about index locality.
// Built-in since Node.js 19+ / all modern browsers
const id = crypto.randomUUID();
console.log(id);
// Example: '9e0c2a4b-8d3f-4b6e-9c1a-1f2b3c4d5e6f'
UUID v7 in JavaScript
v7 embeds a millisecond Unix timestamp in the first 48 bits, so the IDs sort chronologically. This is what modern databases want from a primary key — sequential inserts land in adjacent index pages instead of scattering writes.
// No built-in v7 in Node.js as of 2026. Use 'uuid' package (v9+):
// npm install uuid
import { v7 as uuidv7 } from 'uuid';
const id = uuidv7();
console.log(id);
// Example: '0193a0b8-3e7e-7c91-9d2a-5f6a7b8c9d0e'
Library support
The npm `uuid` package (v9.0.0+, July 2024) exports `v7`. In the browser, the same package works — or paste a 30-line inline implementation that uses `crypto.getRandomValues()`.
Notes
`crypto.randomUUID()` is part of the Web Crypto spec and is the right call for v4 in 2026. Don't use `Math.random()` for IDs — it's not cryptographically secure.
Need them in bulk?
Our UUID generator produces 1 to 1000 UUIDs (v4 or v7) instantly, all in your browser, with one-click copy and JSON/CSV export. Useful for seeding test data or pre-generating IDs offline.