JavaScript UUID Generator (v4, v7 & more)
Instantly generate fast, secure, and unique UUIDs online. Get ready-to-use code for browser (crypto) and Node.js, with or without libraries.
How to Use This Tool
- Select the UUID version (v4, v7) and desired quantity.
- Click "Generate" to instantly create your UUIDs.
- Copy a single UUID by clicking on it, or use the "Copy All" / "Export" buttons.
- Scroll down to the guide for detailed JavaScript code examples for any scenario.
The Ultimate Guide to Generating UUIDs in JavaScript & Node.js
1. Native Browser Method (No Library, Recommended)
// Generate a v4 UUID using the browser's built-in Crypto API.
// This is the fastest, most secure, and recommended method for modern browsers.
const uuid = crypto.randomUUID();
console.log(uuid);
// Example output: "3a67d93a-2j31-4b8c-9c8a-3d23c8a9b12a"
2. Native Node.js Method (v15.6+)
// Use the built-in crypto module in Node.js.
const { randomUUID } = require('crypto');
const uuid = randomUUID();
console.log(uuid);
3. Using the `uuid` Library (Most Versatile)
// First, install the library: npm install uuid
// Then, import the functions you need:
import { v1 as uuidv1, v4 as uuidv4, v5 as uuidv5, v7 as uuidv7 } from 'uuid';
// Generate a v4 UUID (random)
console.log('v4:', uuidv4());
// Generate a v1 UUID (timestamp-based)
console.log('v1:', uuidv1());
// Generate a v7 UUID (timestamp-based, sortable)
console.log('v7:', uuidv7());
// Generate a v5 UUID (namespace-based)
const MY_NAMESPACE = '1b671a64-40d5-491e-99b0-da01ff1f3341';
console.log('v5:', uuidv5('hello.world', MY_NAMESPACE));
4. Generating a UUID from a String (v5)
// v5 is designed to create a predictable UUID from a fixed string within a namespace.
import { v5 as uuidv5 } from 'uuid';
// Define your unique namespace (this is just another UUID)
const MY_NAMESPACE = '1b671a64-40d5-491e-99b0-da01ff1f3341';
const uuid1 = uuidv5('user@example.com', MY_NAMESPACE);
const uuid2 = uuidv5('user@example.com', MY_NAMESPACE);
// uuid1 and uuid2 will always be the same.
console.log(uuid1);
console.log(uuid2);
UUID Version Comparison: How to Choose?
Version | Type | Uniqueness | Recommended Use Case |
---|---|---|---|
UUID v1 | Timestamp + MAC Address | High, but can expose MAC address and time. | Legacy systems or when uniqueness must be guaranteed across distributed systems without a central authority (less common now). |
UUID v4 | Purely Random | Extremely high (from CSPRNG) | The best choice for most applications: user IDs, transaction IDs, etc. |
UUID v5 | Namespace & Name-Based (SHA-1) | Deterministic; same input yields same output. | Generating a reproducible ID from a specific input like a URL or DNS name. |
UUID v7 | Timestamp + Random Value | Extremely high and time-sortable. | Ideal for database primary keys (e.g., in PostgreSQL) to improve indexing performance. |
Frequently Asked Questions (FAQ)
How do I generate a UUID in JavaScript without a library?
For modern browsers and Node.js (v15.6+), use the native crypto.randomUUID()
. It's fast, secure, and built-in.
How can I generate a short UUID?
Standard UUIDs are 36 characters long. If you need a shorter ID, you can use a library like short-uuid
or slice a standard UUID, but be aware that this significantly increases the chance of collisions (duplicates).
What is the difference between `Math.random()` and `crypto.randomUUID()`?
crypto.randomUUID()
uses a cryptographically secure pseudo-random number generator (CSPRNG), making it suitable for security-sensitive applications. Math.random()
is not cryptographically secure and must not be used for anything that requires collision resistance or unpredictability.