How to Generate a UUID in JavaScript
The simplest way to generate a UUID in JavaScript is crypto.randomUUID(), built into every modern browser (Chrome 92+, Firefox 95+, Safari 15.4+) and Node.js (available globally on Node 19+, or via require('crypto') on Node 14.17+/16.7+) with zero dependencies. The c++ uuid reference explains how to generate and parse UUIDs in C++ with proper formatting.
const myUuid = crypto.randomUUID();
console.log(myUuid);
// Output: 550e8400-e29b-41d4-a716-446655440000
const { randomUUID } = require('crypto');
const myUuid = randomUUID();
console.log(myUuid);
// Output: 550e8400-e29b-41d4-a716-446655440000
Explanation
crypto.randomUUID()is available as a global in browsers and on Node.js 19+, so no import is needed in those environments.- On Node.js 14.17+/16.7+, before it became a global,
require('crypto').randomUUID()gives you the exact same function. - It returns a random, RFC 4122 version 4 UUID as a plain 36-character string — not an object — so it drops straight into JSON, URLs, or template literals.
- Under the hood it draws from the platform's cryptographically secure random number generator (the Web Crypto API in browsers, Node's
cryptomodule on the server).
Convert a String to a UUID in JavaScript
JavaScript has no dedicated UUID type — a UUID is just a string — so "converting" one really means validating that a string is well-formed before you trust it. See the python uuid generator page for UUID validation regex, error handling, and framework-specific integration examples.
function isValidUUID(str) {
const regex = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
return regex.test(str);
}
console.log(isValidUUID('550e8400-e29b-41d4-a716-446655440000')); // true
console.log(isValidUUID('not-a-uuid')); // false
import { validate, version } from 'uuid';
const str = '550e8400-e29b-41d4-a716-446655440000';
console.log(validate(str)); // true
console.log(version(str)); // 4
Explanation
- Since a UUID is just a string in JavaScript,
isValidUUID()uses a regex to check the 8-4-4-4-12 hexadecimal pattern. - The
iflag makes the regex case-insensitive, so it matches both uppercase and lowercase hex digits. - The
uuidpackage'svalidate()function checks the same shape but is RFC-aware, andversion()reports which UUID version the string encodes. - Always validate UUIDs coming from user input, URL parameters, or API requests before using them in database queries.
Other Options for Generating UUIDs in JavaScript
crypto.randomUUID() only produces version 4 UUIDs, so reach for the uuid npm package when you need version 1, 5, or 7 identifiers, or support for older browsers and Node.js versions where the native API isn't available.
npm install uuid
import { v4 as uuidv4, v7 as uuidv7 } from 'uuid';
// Version 4 - random, same style as crypto.randomUUID()
const randomId = uuidv4();
// Version 7 - Unix timestamp + random, sortable and index-friendly
const sortableId = uuidv7();
Explanation
npm install uuidadds the package to your project; it works in Node.js and in bundled browser code alike.v4()produces the same style of random UUID ascrypto.randomUUID(), useful as a drop-in for environments where the native API isn't available.v7()encodes the current Unix millisecond timestamp in the most significant bits, producing IDs that sort chronologically — ideal for database primary keys.- The package also exports
v1()(timestamp + node ID) andv5()/v3()(deterministic, namespace-based) for casescrypto.randomUUID()can't cover.