Skip to content

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?

  1. Simplicity: No need for external libraries or complex algorithms.
  2. Security: It uses a cryptographically strong random number generator.
  3. 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

Bootcamp 2025

Join the waiting list