Skip to main content
May 3, 2026 · dev · 4 min read

How to Generate a UUID in JavaScript (and When to Skip the Library)

Learn how to generate a UUID in JavaScript using crypto.randomUUID, manual methods, and when a library like uuid is actually worth adding to your project.

The One-Liner You Probably Already Have

If you're running Node.js 14.17+ or a modern browser, generating a UUID v4 takes exactly one line:

js crypto.randomUUID(); // "3b12f1df-5232-4804-897e-917bf397618a" That's it. crypto.randomUUID() is part of the Web Crypto API, available natively in Chrome 92+, Firefox 95+, Safari 15.4+, and Node.js without any imports. No dependencies, no bundle size cost, cryptographically secure randomness. For most projects, you can stop reading here and go ship something.

But there are real cases where this isn't enough — and a few where developers reach for a library when they genuinely don't need one.

When the Native API Falls Short

Older environments. If you're supporting Node 12, an older Electron shell, or a React Native environment that doesn't polyfill the Web Crypto API fully, crypto.randomUUID() may not exist or may throw in non-secure contexts (HTTP, for example — browsers restrict it to HTTPS and localhost).

UUID v7. The v4 format gives you random IDs with no inherent ordering. UUID v7 embeds a millisecond timestamp in the high bits, making IDs naturally sortable. This matters when you're using UUIDs as primary keys in Postgres or another database — random v4 UUIDs fragment B-tree indexes badly at scale. As of mid-2024, no native browser or Node API generates v7 yet.

Custom formats. Sometimes you need UUIDs in uppercase, stripped of hyphens for a specific API contract, or batched into a file for seed data. The native method gives you one string at a time, lowercase, with hyphens.

Rolling Your Own v4 (Without a Library)

If crypto.randomUUID() isn't available and you'd rather not add a dependency, you can build a v4 UUID manually using crypto.getRandomValues(), which has much broader support:

``js function uuidv4() { return ([1e7] + -1e3 + -4e3 + -8e3 + -1e11) .replace(/[018]/g, c => (c ^ (crypto.getRandomValues(new Uint8Array(1))[0] & (15 >> (c / 4)))) .toString(16) ); } ``

This is the pattern that circulated for years before randomUUID() was standardized. It's a bit opaque, but it's correct and uses the same CSPRNG under the hood. If you want something more readable, the uuid npm package (npm install uuid) does the same thing with a cleaner API and TypeScript types included.

``js import { v4 as uuidv4 } from 'uuid'; uuidv4(); // same output format, same randomness quality ``

Use the package if you need v1, v3, v5, or v7 support in one consistent API. Don't use it just for v4 in a modern environment — that's unnecessary weight.

Bulk Generation and Format Options

Testing and development workflows often need more than one UUID at a time. If you're seeding a database, generating fixture data for Jest tests, or populating a CSV for QA, opening a REPL and running a loop gets tedious fast.

The Bulk UUID v4 Generator on generatorcollection.com lets you generate a configurable list of v4 UUIDs you can copy directly into a seed file or test suite. No setup, no Node version concerns.

For database-friendly identifiers, the UUID v7-Style Generator produces time-ordered UUIDs — the kind that don't wreck your index performance when inserted at high volume. If you're on Postgres and considering using UUIDs as primary keys, v7 is worth understanding before you commit to v4 for everything.

Need uppercase, no-hyphen, or other format variations? The Bulk UUID Generator with Format Options handles those output preferences without you having to write a .replace(/-/g, '').toUpperCase() wrapper every time.

A Quick Decision Framework

Here's how to think about UUID generation in a JavaScript project:

  • Modern browser or Node 14.17+, just need v4? Use crypto.randomUUID(). Done.
  • Need sortable/time-ordered IDs? Use UUID v7. Either write it manually (the spec is public) or use the uuidv7 package.
  • Supporting legacy environments? Use crypto.getRandomValues() with the manual v4 implementation, or add the uuid package.
  • Need bulk output for dev/testing? Use a generator tool rather than writing a loop.
  • Need v3 or v5 (namespace-based, deterministic)? The uuid package is the right call — these aren't random and require a hash function internally.

The uuid package is well-maintained and perfectly fine to use. The point isn't to avoid it — it's to know whether you actually need it, because crypto.randomUUID() being a native method is a relatively recent quality-of-life improvement that a lot of tutorials haven't caught up with yet.

Generate UUIDs Without Writing a Line of Code

If you need UUIDs right now — for a mock API response, a database seed script, or test fixtures — skip the REPL and use the Bulk UUID v4 Generator to get exactly what you need in the format you want, instantly.