How to Generate UUIDs in JavaScript

Generate UUIDs in JavaScript with crypto.randomUUID() for v4 and a package such as uuid for v7 when you need sortable identifiers.

Recommendation: use UUID v4 when you want zero dependencies in modern runtimes, and use UUID v7 when sortable identifiers matter more than pure randomness.

How to generate UUID v4 in JavaScript

const id = crypto.randomUUID();
console.log(id);

How to generate UUID v7 in JavaScript

// npm install uuid
import { v7 as uuidv7 } from 'uuid';

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

Native support notes

  • JavaScript uses UUID terminology across browser and Node-oriented packages.
  • UUID v4 is native in modern environments with Web Crypto support.
  • UUID v7 is typically package-based.
  • Legacy browsers may require a fallback for crypto.randomUUID().

Practical notes

  • Prefer built-in Web Crypto when you only need v4 and your runtime supports it.
  • Use one shared UUID library across frontend and backend packages when you standardize on v7.
  • Read UUID v7 before introducing sortable IDs into database-heavy flows.

Frequently Asked Questions

It is available in modern browsers and runtimes, but older environments may still need a library fallback.

UUID v7 is useful when you want IDs that sort by creation time while still carrying random entropy.

Learn more

These articles expand on related concepts, formats and practical considerations.

By using this site, you agree to our Privacy Policy and Terms of Service. You are not permitted to use the GUIDs (also known as UUIDs) generated by this site or use any other content, services and information available if you do not agree to these terms.
Disclaimer: All information is provided for general educational and technical reference only. While we aim to keep the content accurate, current and aligned with published standards, no guarantees are made regarding completeness, correctness or suitability for any specific use case.
GUID specifications, best practices, security guidance, database behavior and ecosystem conventions (including cloud platforms and identifier formats) may change over time or differ by implementation. Examples, recommendations and comparisons are illustrative and may not apply universally.
This content should not be considered legal, security, compliance or architectural advice. Before making critical design, security or production decisions, always consult the latest official standards and vendor-specific documentation.
Always evaluate behavior in your own environment.
Standards Compliance: The GUIDs generated by this site conform to RFC 4122 and RFC 9562 specifications whenever possible, using cryptographically secure random number generation.