# Generate UUIDs in JavaScript with crypto.randomUUID()

> Learn how to generate a v4 UUID in JavaScript with window.crypto.randomUUID(), a built-in Web Crypto API method that needs no external libraries.

Author: [Flavio Copes](https://flaviocopes.com/about/) | Published: 2024-09-18 | Topics: [Web Platform](https://flaviocopes.com/tags/platform/) | Canonical: https://flaviocopes.com/generating-uuids-in-javascript-with-windowcryptorandomuuid/

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](https://datatracker.ietf.org/doc/html/rfc4122).

Here’s how to use it:

```javascript
const uuid = window.crypto.randomUUID();
console.log(uuid); // e.g. "36b8f84d-df4e-4d49-b662-bcde71a8764f"
```

If you need a few UUIDs right now (or want to compare them with nanoid and ULID), I built a free [ID generator tool](https://flaviocopes.com/tools/id-generator/) for that.
