Try it here
Generate a UUID to paste into your JavaScript code, in any version and format.
Your UUIDs
Copy one, copy them all, or download the list. The format options above apply to everything shown here.
The built in way: crypto.randomUUID
Every modern browser and Node.js 19 or newer can make a v4 UUID with no library at all.
const id = crypto.randomUUID();
// "f47ac10b-58cc-4372-a567-0e02b2c3d479"
In Node the global crypto exists from version 19. On Node 14.17 to 18 import it first: import { randomUUID } from "node:crypto". In browsers it needs a secure context, which means https or localhost. On a plain http page the function is missing and you get a TypeError.
Time ordered v7 and other versions: the uuid package
For anything beyond v4, install the uuid package. It is the standard choice, has no dependencies and works in Node, Deno, Bun and browsers.
npm install uuid
import { v7 as uuidv7, v1 as uuidv1, v5 as uuidv5, v4 as uuidv4 } from "uuid";
uuidv7(); // time ordered, best for database keys
uuidv4(); // random, same as crypto.randomUUID
uuidv1(); // time based, classic
uuidv5("example.com", uuidv5.DNS); // name based, always the same result
Version 7 is what you want for primary keys, because values created later sort later and your index stays compact. See the v7 generator page for why.
Validate and parse
import { validate, version, parse, stringify } from "uuid";
validate("f47ac10b-58cc-4372-a567-0e02b2c3d479"); // true
version("f47ac10b-58cc-4372-a567-0e02b2c3d479"); // 4
parse(id); // Uint8Array of 16 bytes, for binary storage
stringify(bytes); // back to the text form
Without the package, a regular expression does the job for input checking:
const UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
UUID_RE.test(value);
Formatting
id.toUpperCase(); // C# and SQL Server style
id.replace(/-/g, ""); // 32 hex characters
"{" + id + "}"; // braces, Windows registry style
Mistakes to avoid
- Never build a UUID from Math.random. It is not cryptographically random and several engines have visible patterns. Snippets that do this are all over the web. Use crypto.randomUUID or the uuid package.
- Do not use v1 to get ordering. v1 does not sort by time. v7 does.
- Keep the lowercase form. The RFC says output lowercase and accept either case on input. Comparing UUIDs as strings from different sources fails when one side uppercases.
- Store as bytes in the database when you can. 16 bytes instead of 36 characters. Use parse and stringify at the boundary.
Frequently asked questions
Does crypto.randomUUID work in Node.js?
Yes, from Node 14.17 via require('crypto').randomUUID() and as a global from Node 19. It is a v4 UUID, identical to what the uuid package's v4 produces.
How do I generate a UUID v7 in JavaScript?
Install the uuid package and call v7(). There is no built in v7 in browsers or Node yet. The package keeps values monotonic within a millisecond, which is what database keys need.
Is the uuid package safe to use in the browser?
Yes. It uses crypto.getRandomValues, is about 4 KB and has no dependencies. This site's generator runs on it.