DevToolsForYou

UUID Generator in JavaScript / Node.js — Code Examples

UUID Generator in JavaScript / Node.jsUse the online tool →

UUIDs (Universally Unique Identifiers) are 128-bit identifiers used as database primary keys, session tokens, and idempotency keys. UUID v4 (random) is the safe default; UUID v7 (time-ordered) is preferred for database primary keys.

Node.js 14.17+ and all modern browsers include crypto.randomUUID(). For older environments use the uuid package.

Node.js 14.17+ / Modern Browsers
// Built-in — no dependencies needed
const id = crypto.randomUUID();
console.log(id); // e.g. "110e8400-e29b-41d4-a716-446655440000"

// Multiple UUIDs
const ids = Array.from({ length: 5 }, () => crypto.randomUUID());
uuid npm package (v1, v4, v5, v7)
// npm install uuid
import { v4 as uuidv4, v7 as uuidv7 } from "uuid";

const id = uuidv4();
console.log(id);

// UUID v7 — time-ordered, better for database primary keys
const orderedId = uuidv7();
console.log(orderedId);
Notes & gotchas
  • crypto.randomUUID() is available in Node.js 14.17+ and all modern browsers (Chrome 92+, Firefox 95+).
  • UUID v7 produces sortable IDs that reduce B-tree index fragmentation — prefer it for database PKs.
  • Never use Math.random() for UUIDs — it is not cryptographically secure.
Try it in your browser

Need to uuid generator without writing code? The UUID Generator runs entirely in your browser — paste your input and get the result instantly. No signup, no install, no data sent to a server.

Open UUID Generator
UUID Generator in other languages