Generating UUIDs in JavaScript with window.crypto.randomUUID()
If you’ve ever needed to generate unique identifiers in your web applications, window.crypto.randomUUID()
is a method that generates a v4 UUID (Universally Unique Identifier).
It’s part of the Web Crypto API and is supported in all modern browsers.
Why Use It?
- Simplicity: No need for external libraries or complex algorithms.
- Security: It uses a cryptographically strong random number generator.
- Standards-compliant: Generates UUIDs according to RFC 4122.
Here’s how to use it:
const uuid = window.crypto.randomUUID();
console.log(uuid); // e.g. "36b8f84d-df4e-4d49-b662-bcde71a8764f"
→ Here's my latest YouTube video
→ I wrote 17 books to help you become a better developer, download them all at $0 cost by joining my newsletter
→ JOIN MY CODING BOOTCAMP, an amazing cohort course that will be a huge step up in your coding career - covering React, Next.js - next edition February 2025