HiveBrain v1.2.0
Get Started
← Back to all entries
snippetjavascriptTip

Generate a UUID in JavaScript

Submitted by: @import:30-seconds-of-code··
0
Viewed 0 times
generatejavascriptuuid

Problem

A Universally Unique Identifier (UUID) is a 128-bit number used to uniquely identify some object or entity on the Internet. It has been standardized by RFC 4122.
As browsers and Node.js have historically had some differences in their APIs, you should be aware of the runtime environment when generating a UUID. Let's dive into your options and when to use them.
> [!IMPORTANT]
>
> All implementations below are, to the best of my knowledge, compliant with RFC4122 version 4.

Solution

## Older environments

In older environments, generating a UUID is a more complex task. While the technique described below is going to be **less relevant** as time goes by, I think it's still worth documenting.

> [!NOTE]
>
> This technique is the same for both browsers and Node.js. However, in Node.js versions prior to **v17.4.0**, you need to use [`crypto.randomBytes()`](https://nodejs.org/api/crypto.html#cryptorandombytessize-callback) instead of `crypto.getRandomValues()`.

If `crypto.randomUUID()` is not an option, `crypto.getRandomValues()` should still be available. This method allows you to generate **cryptographically strong pseudo-random data**. It accepts a `TypedArray` as an argument and fills it with random values.

Start by creating a string that represents a UUID **template**: `([1e7] + -1e3 + -4e3 + -8e3 + -1e11)`. This string contains **placeholders** (the numbers `0`, `1`, and `8`) that will be replaced with random hexadecimal digits.

Then, using `String.prototype.replace()`, replace the placeholders with the **random hexadecimal digits** that we generate using `crypto.getRandomValues()`. For each matching placeholder, use the **bitwise XOR** operator (`^`) and the **right shift** (`>>`) by the result of `c / 4` (where `c` is the placeholder). This will result in a random hexadecimal digit. Finally, use `Number.prototype.toString()` with a radix of `16` to convert the value to a hexadecimal string.


> [!IMPORTANT]
>
> All implementations below are, to the best of my knowledge, compliant with RFC4122 version 4.
Generating a UUID in a modern browser or Node.js is very straightforward. As the Crypto API has been standardized you can use the crypto.randomUUID() method to generate a UUID.
```js title="Browser"
crypto.randomUUID(); // '7982fcfe-5721-4632-bede-6000885be57d'

Code Snippets

## Older environments

In older environments, generating a UUID is a more complex task. While the technique described below is going to be **less relevant** as time goes by, I think it's still worth documenting.

> [!NOTE]
>
> This technique is the same for both browsers and Node.js. However, in Node.js versions prior to **v17.4.0**, you need to use [`crypto.randomBytes()`](https://nodejs.org/api/crypto.html#cryptorandombytessize-callback) instead of `crypto.getRandomValues()`.

If `crypto.randomUUID()` is not an option, `crypto.getRandomValues()` should still be available. This method allows you to generate **cryptographically strong pseudo-random data**. It accepts a `TypedArray` as an argument and fills it with random values.

Start by creating a string that represents a UUID **template**: `([1e7] + -1e3 + -4e3 + -8e3 + -1e11)`. This string contains **placeholders** (the numbers `0`, `1`, and `8`) that will be replaced with random hexadecimal digits.

Then, using `String.prototype.replace()`, replace the placeholders with the **random hexadecimal digits** that we generate using `crypto.getRandomValues()`. For each matching placeholder, use the **bitwise XOR** operator (`^`) and the **right shift** (`>>`) by the result of `c / 4` (where `c` is the placeholder). This will result in a random hexadecimal digit. Finally, use `Number.prototype.toString()` with a radix of `16` to convert the value to a hexadecimal string.

Context

From 30-seconds-of-code: uuid-generator

Revisions (0)

No revisions yet.